I have a text which might have different newline styles. I want to replace all newlines '\r\n', '\n','\r' with the same newline (in this case \r\n ).
What's the fastest way to do this? My current solution looks like this which is way sucky:
$sNicetext = str_replace("\r\n",'%%%%somthing%%%%', $sNicetext);
$sNicetext = str_replace(array("\r","\n"),array("\r\n","\r\n"), $sNicetext);
$sNicetext = str_replace('%%%%somthing%%%%',"\r\n", $sNicetext);
Problem is that you can't do this with one replace because the \r\n will be duplicated to \r\n\r\n .
Thank you for your help!
If you don't want to replace all Unicode newlines but only CRLF style ones, use:
\R
matches these newlines,u
is a modifier to treat the input string as UTF-8.From the PCRE docs:
and