Forum Moderators: coopster

Message Too Old, No Replies

Reverse of nl2br()

Does such a thing exist?

         

Nick_W

3:02 pm on Nov 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I'd like to strip out the <br />'s put in by nl2br() when I 'edit' an article.

I could just use strip_tags() but then any links etc would also be removed.

Is there any way to do this without sweating over regex for 7hrs?

Many thanks...

Nick

lars stecken

3:29 pm on Nov 19, 2002 (gmt 0)

10+ Year Member



I was thinking the same way until recently. I wanted to strip those TAGS which I added before I stored in the DB.

Then I realized that there's no need to do that if you don't add nl2br() before you store since MySQL is keeping the \n intact. Meaning, when you read the values from the DB THEN add the nl2br().

If this is not really your problem - I think there is no function that reverses nl2br()
Greets

NameNick

3:31 pm on Nov 19, 2002 (gmt 0)

10+ Year Member



Hi Nick,

Maybe you should store the articles in the db as they are and without replacing of \n with <br />. It might be better to do the replacements right before the output for reading on your site. In the meantime you can edit the articles without change and change back.

Otherwise you could use this to replace the html breaks with - nothing: str_replace ("<br />", "", $article);

Maybe you have to escape the / in <br /> to <br \/>. Just test it ;-)

NN

Nick_W

3:56 pm on Nov 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys

Case of not seeing the wood for the trees ;)

You're absolutely right of course, there's no need at all to store the text with <br />'s in it...

Thanks again...

Nick

andreasfriedrich

4:08 pm on Nov 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with Lars. Store the original source with [tags] as markup in the db and do all formatting on the fly when a page is requested.

Use some form of caching to optimize for speed if that is a concern.

Andreas

andreasfriedrich

4:14 pm on Nov 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick, if coding that yourself is too much trouble there is a Basic News Database available that can be installed on any website running PHP and MySQL and itīs just 99 USD. It was written by some guy living in Denmark. You can find it when you google for T* internet and "Basic News Database".

Andreas

Nick_W

4:16 pm on Nov 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cheeky sod ;)

I'm modifying it for a client, and it's been a long time... so you behave yourself!

Nick

kilian guntner

11:58 am on Feb 15, 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.