Forum Moderators: coopster

Message Too Old, No Replies

reverse of nl2br, php

reverse of nl2br, php

         

kilian guntner

11:37 am on Feb 14, 2008 (gmt 0)

10+ Year Member



There is tons of solutions for a reverse of nl2br, a quick, easy understandable and hands on solution would be to use an actual new hard line directly in the code - it should be interpreted as wanted with php 4,5,6.

<?
$string = "Hello <br /><br /><br /><br /> Hello again";

$string = str_replace("<br />","
",$string);
?>

...or to make a more generic regex ofcourse with \n \r and a fluffy acceptance of both <br /> and <br>

Use of this is for example in forms, textareas.

jatar_k

2:14 pm on Feb 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld kilian guntner,

I apologize but I am not sure what your question is

kilian guntner

12:04 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



Ok, lets make the question. Sorry for the un-cronological flow

Is there a reverse of nl2br in php?

---
This is the current top hit on google for search string "reverse of nl2br"

[webmasterworld.com...]

And it was closed yesterday for replies since it was to old.

btw, here is another solution

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

welcome,

K

henry0

12:28 pm on Feb 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



STRIP TAGS [us3.php.net] is not just plain strip tags!
It also takes arguments so you may use it to strip tags and still keep some selected tags;
so in ref to the older thread one does not need a regex (in such a case)

RonPK

3:46 pm on Feb 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're looking for "fluffy acceptance of both <br /> and <br>" without using a regex, try this:

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

kilian guntner

6:40 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



Strip tags does not change <br> to new line feed but only strips out the tags and keeps the ones you want to be left... or even if it does str_replace should be better without regex.

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

Should be the best solution without using regex, thanks Ron.