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
10 2584
You could just remove them -
echo str_replace('\n', ' ' $str);
-
Or am I missing the problem?
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.
(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
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.
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?
Heya, Hanaa.
Try running the string through nl2br() before you display it.
E.g., - echo nl2br($row['question']);
No pbmods, I want none of the escape sequences to be interpreted. not just '\n', others such as '\t' etc.
And, I tried using addslashes() too. The outputs the same even then. The escape characters are being expanded.
Thankyou everyone
It works now. I had to turn magic_quotes_gpc on in php.ini. (PHP 5)
Hanaa
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 %%...
|
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...
|
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. ...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |