[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

\r\n in a textbox

Posted on 2008-11-17
24
Medium Priority
?
450 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:syedasimmeesaq
  • 8
  • 5
  • 4
  • +2
24 Comments
 
LVL 82

Expert Comment

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

Open in new window

0
 
LVL 21

Expert Comment

by:silemone
ID: 22979711
textboxes take literal text...you would have to use:  <br />
0
 
LVL 21

Expert Comment

by:silemone
ID: 22979718
hielo again!!!  we always meet this way...:0P
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22979765
Check the PHP function called nl2br()
http://us2.php.net/manual/en/function.nl2br.php

HTH, ~Ray
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22979778
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
 
LVL 82

Expert Comment

by:hielo
ID: 22979846
perhaps your data contains just \n not \r\n:
echo str_replace("\n","<br />", $str);
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22979871
when I echo my variable

echo $var;

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


I tried all methids but still no go
0
 
LVL 21

Expert Comment

by:silemone
ID: 22979879
so this has to do more with what symbol the db is actually spitting out...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22979886
try this: echo "$var";
0
 
LVL 11

Expert Comment

by:NoiS
ID: 22979988
Try


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

Open in new window

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22979990
that didn't do anything either
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22980010
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
 
LVL 21

Expert Comment

by:silemone
ID: 22980041
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
 
LVL 21

Expert Comment

by:silemone
ID: 22980046
they meaning: Experts-Exchange...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22980049
Yeah, that makes sense.  I'll stand by and wait for clarification and sample output.
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22980057
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22980117
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
 
LVL 82

Expert Comment

by:hielo
ID: 22980122
try this:
echo str_replace('\r\n',"<br />", $str);

notice the apostrophes around \r\n, NOT double quotes
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22980137
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
 
LVL 21

Expert Comment

by:silemone
ID: 22980214
nice job syedasimmee&

0
 
LVL 21

Expert Comment

by:silemone
ID: 22980217
figured it out yourself...good going...
0
 
LVL 11

Expert Comment

by:NoiS
ID: 22980249
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
 
LVL 11

Expert Comment

by:NoiS
ID: 22980275
My Post was late...
0
 
LVL 21

Expert Comment

by:silemone
ID: 22986616
plus Hielo was the reason we got to the solution in the first place...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question