With PCRE regular expressions in PHP, multi-line mode (/m
) enables ^
and $
to match the start and end of lines (separated by newlines) in the source text, as well as the start and end of the source text.
This appears to work great on Linux with \n
(LF) being the newline separator, but fails on Windows with \r\n
(CRLF).
Is there any way to change what PCRE thinks are newlines? Or to perhaps allow it to match either CRLF or LF in the same way that $
matches the end of line/string?
EXAMPLE:
$EOL = "\n"; // Linux LF
$SOURCE_TEXT = "one{$EOL}two{$EOL}three{$EOL}four";
if (preg_match('/^two$/m',$SOURCE_TEXT)) {
echo 'Found match.'; // <<< RESULT
} else {
echo 'Did not find match!';
}
RESULT: Success
$EOL = "\r\n"; // Windows CR+LF
$SOURCE_TEXT = "one{$EOL}two{$EOL}three{$EOL}four";
if (preg_match('/^two$/m',$SOURCE_TEXT)) {
echo 'Found match.';
} else {
echo 'Did not find match!'; // <<< RESULT
}
RESULT: Fail
Did you try the (*CRLF)
and related modifiers? They are detailed on Wikipedia here (under Newline/linebreak options) and seem to do the right thing in my testing. i.e. '/(*CRLF)^two$/m'
should match the windows \r\n
newlines. Also (*ANYCRLF)
should match both linux and windows but I haven't tested this.