"LuKrOz" <lu****@virgilio.it> wrote in message
news:vH*********************@news2.tin.it...
Someone could tell me how can I get the same result substituting
ereg with preg_match and ereg_replace with preg_replace.
$result = ereg("<\[foreach\]>(.+)<\[\/foreach\]>",$this->buffer,$token);
$this->buffer =
ereg_replace("<\[foreach\]>.+<\[\/foreach\]>","<[foreach$i]>",$this->buffer) ;
Thanks.
It doesn't look like you're using anything specific to Posix Extended
regular expressions, so you don't have to do much to convert them to Perl
regular expressions. In Perl regexps, you need some kind of expression
delimiter (and Posix leaves it up to you if you want one):
Posix: "<\[foreach\]>(.+)<\[\/foreach\]>"
Perl: "/<\[foreach\]>(.+)<\[\/foreach\]>/"
It might even work to leave out the expression delimiter altogether, but I
haven't tested it with the preg_*() functions. Have you tried your Posix
pattern in one of the Perl functions?
When you capture, you can just use a simple backreference in the
preg_replace() function:
newstring = preg_replace('/Hello W(.+)\./', 'Goodbye W$1\.', $oldstring);
Old --> New
'Hello World.' --> 'Goodbye World.'
'Hello Wind.' --> 'Goodbye Wind.'
'Hello Sir.' --> 'Hello Sir.'
Translating into Perl regular expressions becomes tough if you start using
Posix character classes, pattern capturing, and look assertions. When
you're just doing basic string matches, the same expressions will usually
work in both.
This may be useful:
http://us3.php.net/manual/en/ref.pcre.php
HTH,
Zac