Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get each line from textarea

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

like image 307
James Avatar asked Sep 13 '10 16:09

James


People also ask

How to make a multi-line text box in HTML?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

Does textarea have value attribute?

<textarea> does not support the value attribute.

How do I count the number of rows in a textarea?

val(); var lines = text. split("\r"); var count = lines.


2 Answers

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.

like image 184
Jim Avatar answered Sep 20 '22 02:09

Jim


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

like image 36
Shah Erhan Avatar answered Sep 22 '22 02:09

Shah Erhan