How do I remove all line breaks in a string in php? And how do I add one?

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

Guest
I want to remove all line breaks in a string in php. And I want to add a linebreak to a string.
 
simple easy way to remove breaks would be to search for them and remove them:

if the string has HTML:
$myString = "<span> there is <br /> no breaks </span>";
$myString = str_replace("<br />", "", $myString);

echo $myString;
// output: <span> there is no breaks </span>

if the string is text search for newline
$myString = "there is \n no breaks";
$myString = str_replace(" \n", "", $myString);

echo $myString;
// output: there is no breaks

You could use preg_replace() and use a single regular expression to search for all line breaks regardless of HTML or plain text, do a google search for regular expression and you will figure that out,

good luck
 
Back
Top