three3
three3

Reputation: 2846

Convert String with Line Breaks into Single Line With Line Breaks Still Intact

I have the following string:

Hello Eric,

Thank you very much for the links. This was exactly what I needed.

Greetings,
John

I am using php's nl2br function to convert new lines into line breaks which is working correctly. After I run the nl2br function on my string, it becomes this:

Hello Eric,<br />

Thank you very much for the links. This was exactly what I needed.<br />

Greetings,<br />
John

So far, this is what I am trying to achieve. However, I now need to convert the new string with line breaks into a single line as so:

Hello Eric,<br />Thank you very much for the links. This was exactly what I needed.<br />Greetings,<br />John

I have tried several different methods but none seem to work. Here are a few things I have tried:

$str = str_replace(array("\r","\n"),"",$str);

$str = str_replace(array("\r\n", "\n", "\r"), "", $str);

$str = preg_replace( "/\r|\n/", "", $str);

None of these work for me though.

Upvotes: 0

Views: 924

Answers (1)

Dorad
Dorad

Reputation: 3682

Try this:

$str = str_replace(array("\r\n","\n"),"",$str);

Instead of

$str = str_replace(array("\r","\n"),"",$str);

Can be watched at: http://sandbox.onlinephpfunctions.com/code/41a6fbd2c937418fce927b31f6ab74ad30f8b4d0

Read more about line breaks characters here: http://en.wikipedia.org/wiki/Newline#Representations

Upvotes: 2

Related Questions