$pee = preg_replace_callback( '/<(script|style|textarea).*?<\/\\1>/s', create_function( '$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);' ), $pee );
$pee = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $pee ); // optionally make line breaks
$pee = str_replace( '<WPPreserveNewline />', "\n", $pee );
$myfunc = function ($matches) { return str_replace("\n", "<WPPreserveNewline />", $matches[0]); }