Link to home
Start Free TrialLog in
Avatar of syedasimmeesaq
syedasimmeesaqFlag for United States of America

asked on

\r\n in a textbox

In my database I have new line starts as this \r\n

now I want to show this data online. The problem is that when I echo it out, instead of showing next line, it just outputs \r\n

how can I fix that so it out put another line and has correct formatting
Thanks
Avatar of hielo
hielo
Flag of Wallis and Futuna image

you need to replace \r\n with br tags:
echo str_replace("\r\n","<br />", $str);

Open in new window

textboxes take literal text...you would have to use:  <br />
hielo again!!!  we always meet this way...:0P
Check the PHP function called nl2br()
http://us2.php.net/manual/en/function.nl2br.php

HTH, ~Ray
Avatar of syedasimmeesaq

ASKER

the problem is that in database it looks like this

ABC
DE
FG

I do not see and \r\n

but when I echo it I see there. I tried heilos method but no go
I am trying rays now.
Thanks
perhaps your data contains just \n not \r\n:
echo str_replace("\n","<br />", $str);
when I echo my variable

echo $var;

it gives me
ABC
\r\nDE
\r\nFG


I tried all methids but still no go
so this has to do more with what symbol the db is actually spitting out...
try this: echo "$var";
Try


echo "<pre>";
echo stripslashes($var);
echo "</pre>";

Open in new window

that didn't do anything either
syedasimmeesaq: What "didn't do anything either?"  Please post what you did and what output you got, OK?  We are just experts, not mind readers!  Thanks, ~Ray
Ray, he was probably on an old page when he was tryin your code...so I think he means yours and didn't see NoiS yet...actually they should probably have a refresh button/trigger to prevent that type of confusion...
they meaning: Experts-Exchange...
Yeah, that makes sense.  I'll stand by and wait for clarification and sample output.
echo "$var"; didn't do anything. Also I made a discovery when I do this
echo str_replace("r","<br />", $var); only then it put it in the next line but the problem is that it still gives a slash before next line and in next line it enters \n

thanks
Also <PRE> didn't work either
ok got it

str_replace("\\r\\n","<br />", $var);

since its closest to heilos suggestion, I think he desrves the points
Thank you all for your help
try this:
echo str_replace('\r\n',"<br />", $str);

notice the apostrophes around \r\n, NOT double quotes
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nice job syedasimmee&

figured it out yourself...good going...
You are missing something;

if your text was saved on database with \r\n and magic_quotes are ON on PHP, it will escape with one more slashe, so, when you echo the $var only one slash will be removed. This is the correct behaviour. But if for some strange reason (possibly an error bettwen POST and retrieve)  when you retrieve the data are putting twice, You need to treat it.

the code below MUST print differente results

echo $var . "<br >";
 
echo stripslashes($var) . "<br >";
 
echo preg_replace("#(\r|\n|\\r|\\n)#","<br />",$var);

Open in new window

My Post was late...
plus Hielo was the reason we got to the solution in the first place...