Jump to content

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?

Edited by johnhenry
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

  • Like 1

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.