Get each line from textarea

PhpParsingTextareaLine

Php Problem Overview


<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.

Php Solutions


Solution 1 - Php

You will want to look into the nl2br() function along with the trim().

The nl2br() will insert <br /> before the newline character (\n) 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); // insert <br /> before \n 

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($textAr, '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.

Solution 2 - Php

You could use PHP constant:

$array = explode(PHP_EOL, $text);

additional notes:

  1. For me this is the easiest and the safest way because it is cross platform compatible (Windows/Linux etc.)
  2. It is better to use PHP CONSTANT whenever you can for faster execution

Solution 3 - Php

Old tread...? Well, someone may bump into this...

Please check out http://telamenta.com/techarticle/php-explode-newlines-and-you

Rather than using:

$values = explode("\n", $value_string);

Use a safer method like:

$values = preg_split('/[\n\r]+/', $value_string);

Solution 4 - Php

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)

Solution 5 - Php

It works for me:

if (isset($_POST['MyTextAreaName'])){
    $array=explode( "\r\n", $_POST['MyTextAreaName'] );

now, my $array will have all the lines I need

    for ($i = 0; $i <= count($array); $i++) 
    {
        echo (trim($array[$i]) . "<br/>");
    }

(make sure to close the if block with another curly brace)

}

Solution 6 - Php

For a <br> on each line, use

<textarea wrap="physical"></textarea>

You will get \ns in the value of the textarea. Then, use the nl2br() function to create <br>s, or you can explode() it for <br> or \n.

Hope this helps

Solution 7 - Php

$content = $_POST['content_name'];
$lines = explode("\n", $content);

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

// $lines contains your lines

Solution 8 - Php

$array = explode("\n", $text);
for($i=0; $i < count($array); $i++)
{
    echo $line;
    if($i < count($array)-1)
    {
         echo '<br />';
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - PhpJimView Answer on Stackoverflow
Solution 2 - PhpShah ErhanView Answer on Stackoverflow
Solution 3 - PhpTemitopeView Answer on Stackoverflow
Solution 4 - PhpshamittomarView Answer on Stackoverflow
Solution 5 - PhpprofimedicaView Answer on Stackoverflow
Solution 6 - PhpFribu - Smart SolutionsView Answer on Stackoverflow
Solution 7 - PhpVincent RobertView Answer on Stackoverflow
Solution 8 - PhpcichyView Answer on Stackoverflow