Using preg_replace
, I need to define the regex PATTERN to match all instances of the newline sequence \r\n
exactly, EXCEPT that occuring at the very end of the text.
preg_replace(PATTERN,'"\r\n"',$text)
How do I specify PATTERN in this case?
Using preg_replace
, I need to define the regex PATTERN to match all instances of the newline sequence \r\n
exactly, EXCEPT that occuring at the very end of the text.
preg_replace(PATTERN,'"\r\n"',$text)
How do I specify PATTERN in this case?
Add a negative lookahead for end-of-line:
preg_replace('/\r\n(?!$)/','"\r\n"',$text)
Note that if you don't have a specific requirement to match \r\n
, you can use \R
, which will match any newline sequence.