Jump to content

String replace with backslash


johnhenry

Recommended Posts

I want to email a post data message and I find that if, when writing the message in a text area and the enter key is pressed, the message has '\r\n' in it. When emailed the '\r\n is shown and there is no new line.

I want to replace the '\r\n' with '<br>' and so I used the following script...

 

$message = str_replace("\r\n","<br>",$message);

 

This does not do a replace. The message stays the same.

 

Can anyone tell me why this doesn't work please?

Link to comment
https://forums.phpfreaks.com/topic/294404-string-replace-with-backslash/
Share on other sites

If you are seeing \r\n then something in your code is applying slashes to escaped characters (most likely from the use of addslashes).

 

However there is no need to replace \r\n with a html break tag yourself. PHP can do this for you check out the nl2br function

"\r\n" is 2 characters (CR LF)

 

'\r\n' is 4 characters (note the single quotes)

 

If you can see \r\n in the string then it's not a CRLF pair and nl2br() won't work, so try your str_replace with single quotes instead of double

Thanks Guru and Moderator.  I have seen the light.

 

I was using $value = mysql_real_escape_string($value) and $value = strip_tags($value)  before the str_replace() function.  Without them it works as expected, although the nl2br suggestion seems like a better way to go.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.