Jump to content

[SOLVED] trying to use nl2br doesnt work


Lodius2000

Recommended Posts

I am using nl2br and a custom str_replace function which should do the same thing as nl2br

but as you can see neither works, check the code

 

<?php

//connect to db so mysql_real_escape_string works

$string = "this
is
'
\"
\
/
a
test";
print $string;

//the above prints:
//    this is ' " \ / a test 

print "<br />\n";

$newstring = mysql_real_escape_string($string);
print $newstring;

//the above prints:
//     this\nis\n\'\n\"\n\\\n/\na\ntest

print "<br />\n";

print nl2br($newstring);

//the above prints:
//     this\nis\n\'\n\"\n\\\n/\na\ntest

print "<br />\n";

$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $newstring);
print $newstr;

//the above prints:
//     this\nis\n\'\n\"\n\\\n/\na\ntest

print "<br />\n";

$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
// Processes \r\n's first so they aren't converted twice.
$newstr2 = str_replace($order, $replace, $str);
print $newstr2;

//the above prints:
//     Line 1<br />Line 2<br />Line 3<br />Line 4<br />

print "<br />\n";

?>

 

Why? what can I do to fix it?

thanks

Link to comment
Share on other sites

 

You are using the variable $newstring in nl2br() and in your first str_replace(). $newstring is escaped and no longer has line-feed. You should use $string and $str instead:

 

<?php

print nl2br($string);

..

..

$newstr = str_replace($order, $replace, $str);

?>

Link to comment
Share on other sites

my bad for not commenting off that first use of, $str it should be.

 

but my point here is to show that no use of nl2br or my str_replace function will turn \r, or \n or \r\n into <br /> on a string escaped with mysql_real_escape_string

 

is there another way that works because i cant figure it out

 

what is particularly troubleing is, print nl2br($newstring); does not do what it is supposed to do, in fact it does nothing and that function is designed to convert data that has been escaped

Link to comment
Share on other sites

ok so moselkady, that worked:

 

"Try the un-escaped string: nl2br($string)"

 

so now how do I un-escape the string that has been put through mysql_real_escape_string back into a string that looks like $string because strip slashes will leave a whole bunch of n's lying around

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.