/**
* This is a function to insure there is only one new line character per line.
* It first replaces all \r chars with \n chars. Then it will replace all
* instances of two or more consecutive new lines (\n) with a single new line feed.
*
* @param $str The string to manipulated.
* @param $normalize
*/
function one_newline_per_line($str=null, $char='NL') {
return preg_replace("/\n\n+/s", "\n", preg_replace("/\r+/s", "\n", $str));
} // END one_newline_per_line()
* This is a function to insure there is only one new line character per line.
* It first replaces all \r chars with \n chars. Then it will replace all
* instances of two or more consecutive new lines (\n) with a single new line feed.
*
* @param $str The string to manipulated.
* @param $normalize
*/
function one_newline_per_line($str=null, $char='NL') {
return preg_replace("/\n\n+/s", "\n", preg_replace("/\r+/s", "\n", $str));
} // END one_newline_per_line()
This function can also be expressed thusly, for clarity's sake:
/**
* This is a function to insure there is only one new line character per line.
* It first replaces all \r chars with \n chars. Then it will replace all
* instances of two or more consecutive new lines with a single new line feed.
*
* @param $str The string to manipulated.
* @param $normalize
*/
function one_newline_per_line($str=null, $char='NL') {
$str = preg_replace("/\r+/s", "\n", $str);
$str = preg_replace("/\n\n+/s", "\n", $str);
return $str;
} // END one_newline_per_line()
* This is a function to insure there is only one new line character per line.
* It first replaces all \r chars with \n chars. Then it will replace all
* instances of two or more consecutive new lines with a single new line feed.
*
* @param $str The string to manipulated.
* @param $normalize
*/
function one_newline_per_line($str=null, $char='NL') {
$str = preg_replace("/\r+/s", "\n", $str);
$str = preg_replace("/\n\n+/s", "\n", $str);
return $str;
} // END one_newline_per_line()
