Solved

issue with \n\r and nl2br

Posted on 2011-02-24
8
458 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 34

Expert Comment

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

Author Comment

by:joomla
Comment Utility

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 34

Expert Comment

by:gr8gonzo
Comment Utility
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
 

Author Comment

by:joomla
Comment Utility
Hi gr8gonzo:
thanks for feedback
unfortunately your suggested didn't work
regards
M
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
What does your code look like right now?
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now