Problem with \r\n, mysql_escape_string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

ok thanx but what would i use?
I couldn't get it too work...

It doesn't want to replace "\r\n" with "\n"... (i tried with) ereg_replace. Somehow it will always be shown wrong in the email.
try to use addslashes instead of mysql_real_string since it replaces every /n with /n/r and /n/r is interpreted properly as a break in my browser(try for compatibility on both IE and Mozilla).

If you still use mysql_real_string() to escape something,,,then you can even use str_replace to change /r/n to /n

Code: Select all

str_replace("/n/r", "/n", $your_message);
you actually shouldn't have to escape anything for the message component, you know..
Actually I have seen only two places where we have to escape a string
1. database insertion
2. url variables.

In other cases, I dont think ',",/ are going to give some problems as feyd said.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rag, my comment was specific to this context. Yes, escaping those are required for many other operations, but in this case, they shouldn't. If anything, you should make sure they are all \r\n characters...
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Post by joachimseitz »

input is always:
test
test

Code: Select all

<FORM method="post">
<textarea name="content"></textarea><INPUT type="submit" name="send">
</FORM>
<?
if ($_POST['content']){
//if I echo $content; here i get: "test\r\ntest"
$content=mysql_escape_string($_POST['content']);
//str_replace i do here!!
echo $content;
mail('j_sendler@arcor.de', 'Feedback', $content, "From: Admin <joachimseitz@hotmail.com>");
}
?>
ps. yours doesnt work but i know what u mean

for:

Code: Select all

$content=str_replace('\r\n', '\n', $content);
i get this in my email:
test\ntest
no new line just \n
:?:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

as I've said, you shouldn't have to do any of that for them in this particular case. Also, know that in single quotes, \r and \n or any of the other "special" metacharacters aren't parsed. You're literally adding '\' and 'n' not a carriage return. Use double quotes to have them parsed.

at most, you may need to use stripslashes() to fix magic_quotes
Post Reply