Page 1 of 1
[SOLVED] splitting textarea lines into separate variables
Posted: Sun Dec 07, 2003 8:12 am
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

Posted: Sun Dec 07, 2003 8:22 am
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
Posted: Sun Dec 07, 2003 8:56 am
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
Posted: Sun Dec 07, 2003 9:07 am
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 />";
?>
Posted: Sun Dec 07, 2003 10:16 am
by zerodegreeburn
thanks worked

Posted: Sun Dec 07, 2003 2:49 pm
by JAM
Just mentioning that [php_man]wordwrap[/php_man]() might be a useful solution also.