\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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
silemoneCommented:
hielo again!!!  we always meet this way...:0P
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
hieloCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.