473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a way to not expand escape sequences?

44 New Member
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 2612
Markus
6,050 Recognized Expert Expert
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 Recognized Expert Expert
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 New Member
(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 Recognized Expert Expert
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_gp c 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 New Member
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_con nect('localhost ', 'root', 'password') or die(mysql_error ());
mysql_select_db ('testd') or die(mysql_error ());
if(isset($_POST['submit']))
{
$query="INSERT INTO tablename(quest ion) VALUES('".$_POS T['question']."')";
$request=mysql_ query($query) or die(mysql_error ());
header("locatio n:testb.php");
}
mysql_close($co nn);
?>

<html>
<head>
<title> Test </title>
</head>
<body>
<form action='testa.p hp' 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_con nect('localhost ', 'root', 'password') or die(mysql_error ());
mysql_select_db ('testd');
$query="SELECT * FROM tablename";
$result=mysql_q uery($query);
while($row=mysq l_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 Recognized Expert Expert
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 New Member
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 New Member
And, I tried using addslashes() too. The outputs the same even then. The escape characters are being expanded.
Jun 27 '08 #9
hanaa
44 New Member
Thankyou everyone
It works now. I had to turn magic_quotes_gp c on in php.ini. (PHP 5)

Hanaa
Jun 27 '08 #10

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

Similar topics

6
6657
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 function that can do this, or do I need to use predefined constants or a table of the values? Below is a sample program. When run with the input w\rx, I want to see the output::
7
96334
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 %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
18
7198
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 QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2 AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2 AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2 AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
3
5725
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. e.g. This is line 1\n\nThis is line 2. When this string is read using a ResourceManager GetString method the string is returned @-quoted, i.e as if I entered the literal @"This is line1\n\nThis is line 2", causing the escape sequences not to be...
3
17556
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 string variable (to hold file path information) that is populated from a textbox. When I view the contents of the variable, while debugging, it shows @"c:\temp\filename.txt". But, when I concatenate it with other string literals or variables using...
5
3107
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 naturally interpreted as escape characters. >>> print "d:\thedir" d: hedir
15
18322
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. Any idea if this is a bug? if (e.KeyChar == '\e') { this.Close(); }
4
4282
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 line with \n\t\t\t\thello\t\t\n would hve the string 'hello' in it. In others words, is there a method of removing all escape sequences from a string? I've tried Regex.Unescape(string) but this doesn't not seem to remove the
5
5334
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 converted to their character representation. They are shown excatly same way than in file. Literals in C# code's variables are shown corretly. Can anyone tell how to read Unicode escape sequences from file so that they
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.