<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.
Use PHP DOM to parse and add
<br/>
in it. Like this:This outputs:
Old tread...? Well, someone may bump into this...
Please check out http://telamenta.com/techarticle/php-explode-newlines-and-you
Rather than using:
Use a safer method like:
You will want to look into the nl2br() function along with the trim().
The
nl2br()
will insert<br />
before the newline character (\n
) and thetrim()
will remove any ending\n
or whitespace characters.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"
To fix it, it would be:
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: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 theexplode()
function will remove the\n
characters.Added the
array_filter()
totrim()
off any extra\r
characters that may have been lingering.For a
<br>
on each line, useYou will get
\n
s in the value of the textarea. Then, use thenl2br()
function to create<br>
s, or you can explode() it for<br>
or\n
.Hope this helps