How can I remove a new line character from a string using PHP?
The line break can be removed from string by using str_replace() function.
Yes it does, see the manual: This function returns a string with whitespace stripped from the beginning and end of str .
Explanation: In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known. $str = "geeks" ; // Or we can write ltrim($str, $str[0]);
$string = str_replace(PHP_EOL, '', $string);
or
$string = str_replace(array("\n","\r"), '', $string);
$string = str_replace("\n", "", $string); $string = str_replace("\r", "", $string);
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