A php variable contains the following string:
<p>text</p> <p>text2</p> <ul> <li>item1</li> <li>item2</li> </ul>
I want to remove all the new line characters in this string so the string will look like this:
<p>text</p><p>text2><ul><li>item1</li><li>item2</li></ul>
I've tried the following without success:
str_replace('\n', '', $str); str_replace('\r', '', $str); str_replace('\r\n\', '', $str);
Anyone knows how to fix this?
The line break can be removed from string by using str_replace() function.
The strip() method will remove both trailing and leading newlines from the string. It also removes any whitespaces on both sides of a string.
Yes it does, see the manual: This function returns a string with whitespace stripped from the beginning and end of str .
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
You need to place the \n
in double quotes.
Inside single quotes it is treated as 2 characters '\'
followed by 'n'
You need:
$str = str_replace("\n", '', $str);
A better alternative is to use PHP_EOL
as:
$str = str_replace(PHP_EOL, '', $str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With