Get each line from a text field

<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 do:

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

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

3) Throw each row into an array.

Important - the text inside textarea can be multilingual.




Tried to use:

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

But that will not work.




Thank.

+43
php parsing textarea line
Sep 13 '10 at 16:29
source share
8 answers

You need to look at nl2br () along with trim () .

nl2br() will insert <br /> before the newline character ( \n ), and trim() will remove any trailing \n characters or whitespace characters.

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

This should do what you want.

UPDATE

The reason the following code will not work is because in order for \n be recognized, it must be inside double quotes, since they use double quote data, where single quotes take it literally, IE "\n"

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

To fix this, it would be:

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

But it is better to use the built-in nl2br() function provided by PHP.

EDIT

Sorry, I decided that the first question was that you could add line strings, indeed, this will change the answer a bit, since anytype explode() will remove 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 this like this, you need to add <br /> to the end of the line before processing is done on its own, since the explode() function will remove the \n characters.

Added array_filter() to trim() with any extra \r characters that may have been delayed.

+88
Sep 13 '10 at 16:33
source share

You can use constant PHP:

 $array = explode(PHP_EOL, $text); 

additional notes:
1. For me, this is the easiest and safest way, because it is compatible with cross-platform (Windows / Linux, etc.)
2. Better use PHP CONSTANT when you can speed up execution

+30
Oct 24 2018-11-11T00:
source share

Old tread ...? Well, someone might run into this ...

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

Instead of using:

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

Use a more secure method, for example:

 $values = preg_split('/[\n\r]+/', $value_string); 
+8
Jan 31 '13 at 15:55
source share

Use the PHP DOM to parse and add <br/> to 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); 

It is output:

 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) 
+4
Sep 13 '10 at 16:39
source share

With a <br> on each line, use

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

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

Hope this helps

+3
Sep 13 '10 at 16:33
source share

This 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/>"); } 

(don't forget to close the if block with another curly brace)

 } 
+3
Nov 13 '11 at
source share
 $array = explode("\n", $text); for($i=0; $i < count($array); $i++) { echo $line; if($i < count($array)-1) { echo '<br />'; } } 
+2
Sep 13 '10 at 16:33
source share
 $content = $_POST['content_name']; $lines = explode("\n", $content); foreach( $lines as $index => $line ) { $lines[$index] = $line . '<br/>'; } // $lines contains your lines 
+2
Sep 13 '10 at 16:44
source share



All Articles