473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Is there a way to not expand escape sequences?

44
Hello there.

[PHP]$str="This is \na ball";
echo $str;[/PHP]

Is there a way i can make the text to be as is, without expanding the escape sequences. I know that single quoted strings do not expand escape sequences. But I need to echo text that's been entered by the user in a html textarea as is. And thats why I dont want the escape sequences to be expanded.

Thanks,
Hanaa
Jun 25 '08 #1
10 2584
Markus
6,050 Expert 4TB
You could just remove them
Expand|Select|Wrap|Line Numbers
  1. echo str_replace('\n', ' ' $str);
  2.  
Or am I missing the problem?
Jun 25 '08 #2
Atli
5,058 Expert 4TB
You want it to show the escape characters... Like \" instead of "?

If so, try running it through the addslashes function before printing it.
Jun 26 '08 #3
hanaa
44
(I am sorry if i was unclear.)
That would remove the '\n'. I want the '\n' to be printed as is.
It isnt just the escape sequence '\n'. Say, if the text that the user's entered contains '\n' or '\r' or '\w'(which is not an escape sequence), I want all the text to be echoed as is.
There's this textarea, where the user is supposed to enter some text which could contain words like '\none' (which cud contain escape sequences that are not to be interpreted). but i when i try to display the entered text on another page, all the escape sequences are being interpreted. But I want the text to be echoed exactly how the user wrote it.
Or do i have to use str_replace to replace all the escape sequences..? replace '\n' with '\\n' and likewise for all the other escape sequences?

Hanaa
Jun 26 '08 #4
Atli
5,058 Expert 4TB
How are you printing the text. Could you show us the code?

PHP does not threat the \ character inside strings as and escape character unless it is being used from within a PHP script. So user input should not be parsed into their respective special characters by PHP.

Keep in mind tho, that if your user enters something like: "I am \not two lines", that exact text will be printed to the browser.
The browser, however, may very well convert the \n into a new-line char.

By using the addslashes function, you will in fact be converting that string into "I am \\not two lines", which your browser should parse into "\n" rather than a new-line char.

Edit.
Be careful tho. If the magic_quotes_gpc directive is enabled in your PHP installation, the addslashes function will automatically be invoked on all user input before you get it.
So if that is the case, calling it again will add extra backslashes.
Jun 26 '08 #5
hanaa
44
Thanks Atli and markusn00b.
It just occured to me that i forgot to mention that this has to do with MySQL too. Just posting a text to display it on another page, is indeed echoing the exact text(which is what i want). But when i save it in a database, retrieve and echo it, the escape characters are being interpreted. Here's the code. This isnt the page that i made. In those that i made, i ve used the same page to perform all edit, delete, add functions, so its messy. So i wrote down this.

testa.php
[PHP]<?php
$conn=mysql_connect('localhost', 'root', 'password') or die(mysql_error());
mysql_select_db('testd') or die(mysql_error());
if(isset($_POST['submit']))
{
$query="INSERT INTO tablename(question) VALUES('".$_POST['question']."')";
$request=mysql_query($query) or die(mysql_error());
header("location:testb.php");
}
mysql_close($conn);
?>

<html>
<head>
<title> Test </title>
</head>
<body>
<form action='testa.php' method='post'>
Enter text<br/>
<textarea name='question' rows='5' cols='60'></textarea>
<input type='submit' name='submit' value='Go!'>
</form>
</body>
</html>[/PHP]

testb.php
[PHP]<html>

<head>
</head>
<body>
<?php
$conn=mysql_connect('localhost', 'root', 'password') or die(mysql_error());
mysql_select_db('testd');
$query="SELECT * FROM tablename";
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result))
{
echo $row['question'];
echo"<br/>";
}
?>
</body>
</html>
[/PHP]

Eg.
I enter the followin text on the first page-
This is a \n bat.
And on the next page i see whats below
This is a bat.

Am i supposed to post this in the MySQL section?
Jun 26 '08 #6
pbmods
5,821 Expert 4TB
Heya, Hanaa.

Try running the string through nl2br() before you display it.

E.g.,
Expand|Select|Wrap|Line Numbers
  1. echo nl2br($row['question']);
Jun 26 '08 #7
hanaa
44
No pbmods, I want none of the escape sequences to be interpreted. not just '\n', others such as '\t' etc.
Jun 27 '08 #8
hanaa
44
And, I tried using addslashes() too. The outputs the same even then. The escape characters are being expanded.
Jun 27 '08 #9
hanaa
44
Thankyou everyone
It works now. I had to turn magic_quotes_gpc on in php.ini. (PHP 5)

Hanaa
Jun 27 '08 #10
Markus
6,050 Expert 4TB
Thankyou everyone
It works now. I had to turn magic_quotes_gpc on in php.ini. (PHP 5)

Hanaa
Great news, Hanaa!

See you around.
Jun 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Walter L. Preuninger II | last post by:
I need to convert escape sequences entered into my program to the actual code. For example, \r becomes 0x0d I have looked over the FAQ, and searched the web, with no results. Is there a...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
3
by: Ken | last post by:
HI: I'm reading a string that will be displayed in a MessageBox from a resource file. The string in the resource file contains escape sequences so they will be broken up into multiple lines. ...
3
by: Don | last post by:
I am building a string from a combination of hardcoded string literals and user input (via textbox). I know about using @"c:\temp\filename.txt" to ignore escape sequences. Now let's say I have a...
5
by: nummertolv | last post by:
Hi, My application is receiving strings, representing windows paths, from an external source. When using these paths, by for instance printing them using str() (print path), the backslashes are...
15
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN....
4
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
5
by: John Ztwin | last post by:
Hello, I have a file that contains ordinary text and some special charaters in Unicode escape sequences (\uxxxx). When I read the file using e.g. StreamReader Unicode escape sequences are not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.