I want to replace multiple newline characters with one newline character, and multiple spaces with a single space.
I tried preg_replace(\"/\\n\\n+/\", \"\\n\",
This is the answer, as I understand the question:
// Normalize newlines
preg_replace('/(\r\n|\r|\n)+/', "\n", $text);
// Replace whitespace characters with a single space
preg_replace('/\s+/', ' ', $text);
This is the actual function that I use to convert new lines to HTML line break and paragraph elements:
/**
*
* @param string $string
* @return string
*/
function nl2html($text)
{
return '' . preg_replace(array('/(\r\n\r\n|\r\r|\n\n)(\s+)?/', '/\r\n|\r|\n/'),
array('
', '
'), $text) . '
';
}