[SOLVED] nl2br

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rahil
Forum Newbie
Posts: 17
Joined: Sun Feb 15, 2004 2:24 pm
Location: Toronto, Ontario, Canada

[SOLVED] nl2br

Post by Rahil »

How can I un-nl2br something? Like, for a news script, when I'm adding news in a textarea, I want it to nl2br then put it in the database. Then on my news page, I want it to echo what ever is in the database. Now, if I want to edit the news post, I go back to the textarea, but I don't want to see the <br /> and other HTML. How could i un-nl2br it? Thanks.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just don't nl2br() it before putting it in the database. Only nl2br() on the news page before echoing it out, the textarea on the edit page will then still show the original newlines (that are in the db).
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post by Ixplodestuff8 »

Mark's idea is best, but incase it's too late and your news is already with the br's try this:

str_replace ( '<br />', '', $the_thingy );

it will get rid of all <br />'s, the only downside is that if a <br /> was entered manually and not by nl2br it'll get cut off aswell
Rahil
Forum Newbie
Posts: 17
Joined: Sun Feb 15, 2004 2:24 pm
Location: Toronto, Ontario, Canada

Post by Rahil »

Thanks guys. Problem solved :D
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Ixplodestuff8 wrote:Mark's idea is best, but incase it's too late and your news is already with the br's try this:

str_replace ( '<br />', '', $the_thingy );

it will get rid of all <br />'s, the only downside is that if a <br /> was entered manually and not by nl2br it'll get cut off aswell
To take that a step further:

Code: Select all

<?php
function un_nl2br ($string)
{
    $string = str_replace ("<br />", "\n", $string);
    return $string;
}
?>
Post Reply