\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
LVL 9
syedasimmeesaqAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
LOL. We posted at the same time. This:
str_replace("\\r\\n","<br />", $var);

and this:
echo str_replace('\r\n',"<br />", $str);

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

Open in new window

0
 
silemoneCommented:
textboxes take literal text...you would have to use:  <br />
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
silemoneCommented:
hielo again!!!  we always meet this way...:0P
0
 
Ray PaseurCommented:
Check the PHP function called nl2br()
http://us2.php.net/manual/en/function.nl2br.php

HTH, ~Ray
0
 
syedasimmeesaqAuthor Commented:
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
0
 
hieloCommented:
perhaps your data contains just \n not \r\n:
echo str_replace("\n","<br />", $str);
0
 
syedasimmeesaqAuthor Commented:
when I echo my variable

echo $var;

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


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


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

Open in new window

0
 
syedasimmeesaqAuthor Commented:
that didn't do anything either
0
 
Ray PaseurCommented:
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
0
 
silemoneCommented:
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...
0
 
silemoneCommented:
they meaning: Experts-Exchange...
0
 
Ray PaseurCommented:
Yeah, that makes sense.  I'll stand by and wait for clarification and sample output.
0
 
syedasimmeesaqAuthor Commented:
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
0
 
syedasimmeesaqAuthor Commented:
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
0
 
hieloCommented:
try this:
echo str_replace('\r\n',"<br />", $str);

notice the apostrophes around \r\n, NOT double quotes
0
 
silemoneCommented:
nice job syedasimmee&

0
 
silemoneCommented:
figured it out yourself...good going...
0
 
NoiSCommented:
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

0
 
NoiSCommented:
My Post was late...
0
 
silemoneCommented:
plus Hielo was the reason we got to the solution in the first place...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.