Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

issue with \n\r and nl2br

Posted on 2011-02-24
8
Medium Priority
?
468 Views
Last Modified: 2012-05-11
I'm passing a variable in my URL
index.php?p=this\r\ntext

using $_GET[var] I'm getting this\\r\\ntext

why is \r\n being modified to \\r\\n

nl2br() won't work in my variable

replace_sub("\\r\\n","<br/>", $var) doesn't work either ?

can you help?
0
Comment
Question by:joomla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 34975333
Try:
$var = str_replace("\\\\r\\\\n","<br/>",$var);
0
 

Author Comment

by:joomla
ID: 34976328

this is how far I seem to get
index.php?p=separate\r\nlines

$var=$_GET[var];

what $var gets is "\\r\\n"   (don't understand why the change.   as I inputed "\r\n")

$unwanted=array("\r","\n","\r\n","\\r\\n","\\r\\n");
$wanted="<br/>";
$var= str_replace($unwanted,$wanted,$var);

results in $var = separate<br/>lines

when I put this in the text area it shows
separate<br/>lines

I wanted it to show
separate
lines

thanks for any help


0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 34976718
If you want it on separate lines in a text area, you don't need to use <br/> - text areas pay attention to line breaks. Just change $wanted = "\r\n"; and you should see what you're looking for.

The problem is that by default, PHP tries to automatically escape special characters that are entered via the query string. It's a security thing, which you can turn off, but  that's not recommmended. It's better to simply fix any specific cases like this.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:joomla
ID: 34976734
Hi gr8gonzo:
thanks for feedback
unfortunately your suggested didn't work
regards
M
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 34976741
What does your code look like right now?
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34977428
Step by step. Does this work:


<?php
$var=$_GET[p];

//what $var gets is "\\r\\n"   (don't understand why the change.   as I inputed "\r\n")

$unwanted="\\\\n";
$wanted="\n";
$var= str_replace($unwanted,$wanted,$var); 

$unwanted="\\\\r";
$wanted="\r";
$var= str_replace($unwanted,$wanted,$var); 

?>
<textarea><?php echo"$var";?></textarea>

Open in new window

0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34992480
This is simple codes and should be working ...
NOTICE the way I use single quotes and double quotes in str_replace
$var = $_GET['p'];
$var = stripslashes($var);
// Turning \r\n to breakspace in textarea
echo '<textarea>'.str_replace('\r\n', "\r\n", $var).'</textarea>';

Open in new window


Regards...
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34992504
And here's for the learning purposes (for those who wonder) :-)
<?php

// \r\n on single quotes
$var1 = 'NO break \r\n space';
// \r\n on double quotes
$var2 = "YES break \r\n space";

// echo $var1 in TEXTAREA
echo 'Var#1: <textarea>'.$var1.'</textarea>';
// echo $var2 in TEXTAREA
echo 'Var#2: <textarea>'.$var2.'</textarea>';

?>

Open in new window

0

Featured Post

Technology Partners: 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 look for a specific file type in a local or remote server directory using PHP.

722 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