Forum Moderators: coopster
I've fighting with preg_replace for a while already, it fails to match either anything or only the first alternative in the rgeular expression. I've tried to change/define almost all the modifiers, with no luck. Can anyone tell me what the hell I've done wrong?
$patterns = '/^((1stalternative¦2ndalternative¦3rdalternative¦4rdalternative).*)$/iD';
$replacements = '<bold>\\1</bold>';
$output = preg_replace($patterns,$replacements,$input);
blablablabla
[i]2ndalternative[/i]:
blablablabla
blablablabla
[i]4ndalternative[/i] something
blablablabla
blablablabla
[i]2NDALTERNATIVE[/i]:
blablablabla
blablablabla
$patterns = '/(1stalternative¦2ndalternative¦3rdalternative¦4rdalternative)/i';
$replacements = '<bold>\\0</bold>';
blablablabla
2ndalternative:
blablablabla
blablablabla
4thalternative something
blablablabla
blablablabla
2NDALTERNATIVE:
blablablabla
bla1srAlternativeblablabla
In the meantime I've checked the pattern modifiers and noticed that I missunderstood the meaning of the m modifier - which is what I need right now. By default the input string is threated as a single line, not the BOL not the EOL anchors matches within the text, only at the begining of the text and the end of the text regardless of how many newline chars I have in the text. But if I put the m modifier there, both of the anchors matches proprely.
So thanks for your help anyway, it seems to be working well.
match1
blabla
match2
blabla
'/^(match.*)$/m' '<smthg>\\1</smthg>' <smthg>match1
</smthg>
blabla
<smthg>match2
</smthg>
blabla
PCRE_MULTILINE? Won't happen, at least not through any modifiers. The PCRE docs specifically state newline (\n). You would have to either get rid of them yourself with a regex or string replacement function. Or, if you think it may be in the data as an optional end of line terminator as described, alter your regex...
"/^(match[^\r])/m"
Anyway, thanks for your help