Forum Moderators: coopster

Message Too Old, No Replies

Sharing findings about nl2br()

For those scripting their CMS

         

henry0

7:19 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few days ago in a thread CMS related a tip about nlbr2() was offered.

This could generate headaches if after updating you re-display the “updates” in the editors and if the form is resubmitted.
The result becomes ugly: Any time it is re-submitted a new set of <br /> is added!

So the trick is to undo the <br /> at the editor output level by using
For ex:
$patterns[o]= ‘/<br \/>/’;
$replace[0]= ‘ ‘;
$test=pre_replace($patterns, $replace, $test);

Or is there another function that undo nlbr2() like code, encode…

coopster

7:42 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why would you use nl2br() if you are writing the data to a <textarea> element? No need to do that, just leave the newlines. If you are displaying HTML, then you would want to use that function.

coopster

7:44 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



An example, demonstrating the difference (for clarification).
<?php $string = "My\ntext\nis\nhere."; ?> 
<textarea><?php print $string; ?></textarea>
<p><?php print nl2br($string); ?></p>

henry0

7:50 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I over simplified my example
and I use some HTML elements
thanks

coopster

7:56 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I apologize if I am misunderstanding you Henry, just trying to make the connection. I guess I just wanted to demonstrate that if you are writing the data back out to a textarea for more editing, don't use nl2br(). However, if you want the newlines to come out correctly in HTML (outside of using <pre> elements) like in a paragraph as shown above, then at that time you would want to use the nl2br() function.

To answer the original question though, no -- to the best of my knowledge there is no built-in function to remove the <br />. You would have to write your own regular expression.

However, if you use the content-editing area as described above, you won't have to worry about them.

henry0

9:54 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I apologize too (even more!)
I type as I think, and do not prooooof enough.

Resulting in kind of cryptic post