Got a problem with preg_replace in a textarea. “$” or the “m” modifier are not working properly here:
<?php
$text = '1 - 2 - 3
a - b - c
foo - bar - baz';
$text_replaced = preg_replace('/^(.*) - (.*) - (.*)$/m', '$1 - $2 "$3"', $text);
echo '
<textarea rows=20 cols=20>
'.$text_replaced.'
</textarea>
';
should return
1 - 2 "3"
a - b "c"
foo - bar "baz"
but it returns
1 - 2 "3
"
a - b "c
"
foo - bar "baz"
How can this be solved?
Try yourself: http://codepad.viper-7.com/LqgDHg
By default
.matches everything apart from\n(LF). You however use Windows style\r\n(CRLF) line breaks. Thus\ris included in the match.What you probably want is this:
The
(*ANYCRLF)modifier changes the meaning of.towards accepting all characters apart from\rand\n.