[thelist] PHP REg Exp

Struan Donald struan at exo.org.uk
Tue Dec 18 09:20:30 CST 2007


* at 18/12 17:28 +0300 Fred Jones said:
> I am stuck with a simple Reg Exp. I want to do this:
> 
> preg_replace('/(.*)__START_STYLE__([.,\n]*)__END_STYLE__(.*)/','<style>$2 
> </style>',$body);
> 
> meaning to scan the multiline string $body, find __START_STYLE__, find 
> __END_STYLE__ and store any and all characters between them in $2.

As you say . matches everything but newlines. However there's a set
of pattern modifiers that change this, and other, behaviours of the
regular expression:

http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

So, if you instead use this pattern:

/(.*)__START_STYLE__(.*)__END_STYLE__(.*)/s

( note the trailing s )

then it should do what you want. 

Struan



More information about the thelist mailing list