[SOLVED] splitting textarea lines into separate variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

[SOLVED] splitting textarea lines into separate variables

Post by zerodegreeburn »

How would i go about making each line in a text area (defined by a line break obviously not by the width of textarea) be a separate variable

like $line1, $line2 etc

it's got me foxed :S

anyone? thanks :)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Take a look at explode().

Example:

Code: Select all

<?php

$text = <<< TEXT
look
im
on
different
lines!
TEXT;

$lines = explode('\n', $text);

echo $lines[0];

?>
-Nay
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

ok, thanks :)

but i really want each line in it's own variable, for some reason the code above gives all the lines in $lines[0] not just the first line, whereas on php.net it shows taht you can define each line with the pizza pieces example.

i'm doing it this stupid way because i want to add bits of text at the beginning of each line, like an ASCII design (it's a border on the left).

also, how would i count the total number of lines?

thanks a lot
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Try exploding the text using \r\n and not just \n....

Code: Select all

<?php

$lines = explode("\r\n", $text);

echo "Number of lines/vars = ".count($lines)."<br />";
echo "<hr />";

foreach($lines as $key => $value)
echo $key." - ".$value."<br />";

?>
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

thanks worked :D
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Just mentioning that [php_man]wordwrap[/php_man]() might be a useful solution also.
Post Reply