View Full Version : Undoing nl2br
Does anyone know if it is possible to undone nl2br. I am writing a forum and want to be able to allw users to edit their posts in a text area box. As the message has been 'nl2br' before inserted into the database, all the </br> show up.
bcarl314
10-22-2002, 04:26 PM
Just a guess, but did you try something like this:
$myString = strtr($myString, "<br />", "\n");
jeske
10-22-2002, 07:24 PM
Hi,
Another option is to submit to the database without using nl2br, leaving the newlines intact. Then use nl2br when retrieving and formatting the data for display. That way, the newlines stay intact when the data is pulled back in a text box for editing.
Jeff
beetle
10-22-2002, 08:50 PM
jeske's suggestion (as is my own) should be the way it's done. If you read the page on nl2br() in the PHP manual (http://www.php.net/manual/en/function.nl2br.php), they talk about this very concept, and why there isn't a br2nl() function.
beetle
10-22-2002, 08:58 PM
Oh, and if you do want a function to do this, better use preg_replace instead of strtr as bcarl314 suggested. strtr translates characters and treats the string arguments as arrays...so $myString = strtr($myString, "<br />", "\n");
will populate $mystring with "\nr />". Not exactly what we want.
Example of preg_replace()$myString = preg_replace("/<br( \\/)?>/", "\n", $myString);
firepages
10-23-2002, 02:24 AM
I would suggest a simple
$str=str_replace('<br />','',$str);
is plenty , would not suggest regex unless you really need it (though how str_replace is handled internally I dont know)
To add yet another note - if you use single quotes around function parameters you can use a hard typed newline.
$data = str_replace('<br />','
',$data);
would be perfectly valid.
Originally posted by Ökii
To add yet another note - if you use single quotes around function parameters you can use a hard typed newline.
$data = str_replace('<br />','
',$data);
would be perfectly valid.
Thanks to you all! I have gone with Okii suggestion and it works fine, but thanks to everyone else for their suggestions. :thumbsup:
russia5
09-06-2006, 05:31 PM
Take a look at PHP Freaks code snippets.
Fumigator
09-06-2006, 05:34 PM
You're... digging up a four year old post that was resolved? heh. I only do that when I'm drunk.
lavinpj1
09-06-2006, 06:15 PM
Haha. Gotta be mighty drunk to dig THAT deep.
~Phil~
Mwnciau
09-06-2006, 06:25 PM
function br2nl($text)
{
return preg_replace('/<br\\\\s*?\\/??>/i', "\\n", $text);
}
That was in the manual.
marek_mar
09-06-2006, 06:33 PM
Odd. That won't work.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.