470,855 Members | 1,278 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Regular Expression

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.
Jul 16 '05 #1
1 2597
"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
Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
25 posts views Thread by Mike | last post: by
1 post views Thread by NvrBst | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.