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