php remove newline from string

You can remove new line character any where in string using regex preg_replace.preg_replace is function which work on regex to search and replace in string.Regular express is strong part of php.You can also search replace html tags.Here I have used regex string feature to search and replace new line character.

Here is example how you can remove new line anywhere in php

$str = <<<EOD
Example of string\n example string\n example string.\n example string.\n example string.\n example string.\n\n\n\n niks
EOD;
 
file_put_contents('with_new_line.txt',$str);
$str = preg_replace("/\n/", '', $str);
file_put_contents('without_new_line.txt',$str);

Leave a Reply

Your email address will not be published. Required fields are marked *