How to remove line breaks (no characters!) from the string?
$str = "
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br />
<br />
Greetings,<br />
Bill";
echo str_replace(array("\n", "\r"), '', $str); // echo $str in a single line
You should be able to replace it with a preg that removes all newlines and carriage returns. The code is:
preg_replace( "/\r|\n/", "", $yourString );
Even though the \n
characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.
Ben's solution is acceptable, but str_replace() is by far faster than preg_replace()
$buffer = str_replace(array("\r", "\n"), '', $buffer);
Using less CPU power, reduces the world carbon dioxide emissions.
It's because nl2br()
doesn't remove new lines at all.
Returns string with
<br />
or<br>
inserted before all newlines (\r\n
,\n\r
,\n
and\r
).
Use str_replace
instead:
$string = str_replace(["\r\n", "\r", "\n"], "<br />", $string);