views:

255

answers:

5
<textarea> put returns between paragraphs
for linebreak add 2 spaces at end
indent code by 4 spaces
quote by placing > at start of line
</textarea>

$text = value from this textarea;

How to:

1) Get each line from this textarea ($text) and work with them using foreach()?

2) Add <br /> to the end of each line, except the last one?

3) Throw each line to an array.

Important - text inside textarea can be multilanguage.


Have tried to use:

$text = str_replace('\n', '<br />', $text);

But it doesn't work.


Thanks.

+1  A: 

for
on each line, just write <textarea wrap="physical"></testarea>, you will get "\n" then use the nl2br() function to create

or you can, get value of textarea and explode() it for "
" or "\n"

i hope it helps

Fincha
thanks for this, its new for me.
Happy
+1  A: 
$array = explode("\n", $text);
for($i=0; $i < count($array); $i++)
{
    echo $line;
    if($i < count($array)-1)
    {
         echo '<br />';
    }
}
cichy
+2  A: 

You will want to look into the nl2br() function along with the trim(). The nl2br() will replace the newline character (\n) with <br /> and the trim() will remove any ending \n or whitespace characters.

$text = trim($_POST['textareaname']); // remove the last \n or whitespace character
$text = nl2br($text); // replace \n with <br /> 

That should do what you want.

UPDATE

The reason the following code will not work is because in order for \n to be recognized, it needs to be inside double quotes since double quotes parse data inside of them, where as single quotes takes it literally, IE "\n"

$text = str_replace('\n', '<br />', $text);

To fix it, it would be:

$text = str_replace("\n", '<br />', $text);

But it is still better to use the builtin nl2br() function, PHP provides.

EDIT

Sorry, I figured the first question was so you could add the linebreaks in, indeed this will change the answer quite a bit, as anytype of explode() will remove the line breaks, but here it is:

$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($text, 'trim'); // remove any extra \r characters left behind

foreach ($textAr as $line) {
    // processing here. 
} 

If you do it this way, you will need to append the <br /> onto the end of the line before the processing is done on your own, as the explode() function will remove the \n characters.

Added the array_filter() to trim() off any extra \r characters that may have been lingering.

Brad F Jacobs
what about the first question?
Happy
The above code is assuming that you are grabbing the data from the textarea using a form you setup. If you have the HTML data and you would like to parse that, see shamittomar's answer.
Brad F Jacobs
why double quotes, whats the magic?
Happy
@premiso I want to parse my own textarea, from $_POST
Happy
The reason for the double quotes is that they parse data inside the string, where as single quotes takes data literally.
Brad F Jacobs
No, `explode` doesn't use regular expressions. If you want to split strings based on RE, use `preg_split` instead.
poke
Happy, I did an edit, you can do an `array_filter` with `trim` which will trim off any extra `\r` characters that were left behind. Or as poke said, use the `preg_split`.
Brad F Jacobs
ok, thanks for the point!
Happy
+2  A: 

Use PHP DOM to parse and add <br/> in it. Like this:

$html = '<textarea> put returns between paragraphs
for linebreak add 2 spaces at end
indent code by 4 spaces
quote by placing > at start of line
</textarea>';

//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('textarea');

//get text and add <br/> then remove last <br/>
$lines = $nodes->item(0)->nodeValue;

//split it by newlines
$lines = explode("\n", $lines);

//add <br/> at end of each line
foreach($lines as $line)
    $output .= $line . "<br/>";

//remove last <br/>
$output = rtrim($output, "<br/>");

//display it
var_dump($output);

This outputs:

string ' put returns between paragraphs
<br/>for linebreak add 2 spaces at end
<br/>indent code by 4 spaces
<br/>quote by placing > at start of line
' (length=141)
shamittomar
+1 as he may be wanting to parse HTML, and if so this is a better way of doing it. But not sure if that is his intentions.
Brad F Jacobs
thanks man, good solution for parsing.
Happy
I'm parsing my own textarea from $_POST
Happy
@downvoter, Why the downvote ? Please enlighten.
shamittomar
+1  A: 
$content = $_POST['content_name'];
$lines = explode("\n", $content);

foreach( $lines as $index => $line )
{
    $lines[$index] = $line . '<br/>';
}

// $lines contains your lines
Vincent Robert
thanks Robert, can you explain this part of code "$index => $line"?
Happy
foreach( $lines as $index => $line ) == "for each element of $lines, iterate with $index containing the key/index and $line containing the value" http://fr.php.net/manual/en/control-structures.foreach.php ... (Actually, my first name is Vincent ^^)
Vincent Robert
sorry Vincent, thanks for the comment.
Happy