preg_replace and multiple lines?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

preg_replace and multiple lines?

Post by patrikG »

I want to replace text with preg_replace that necessarily includes line-breaks.

How can I have preg_replace support multiple lines, i.e. line-break ("\r\n"). For some reason the parameters "\m" (as suggested on php.net) and "\s" don't work, and including an optional \r\n doesn't do it either - it all works fine with preg_match_all but not with preg_replace

Surely there must be a more elegant solution than to make it a one-line string by doing a preg_split on the string at the line-breaks and imploding it with a linebreak-marker then replace things and put the linebreaks back in?

Hmmm...help? :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Bah. Evil Madmen invented regEx?

Somehow I got it to work, I don't remember what I did.

"/({IF [a-zA-Z_]+ ((=)?(.+|[\r\n]))})/sU"

Great. The sun is shining outside as if it was full blown summer and that's what my afternoon looked like: "/({IF [a-zA-Z_]+ ((=)?(.+|[\r\n]))})/sU".

:twisted:
Post Reply