NYCPHP Meetup

NYPHP.org

[nycphp-talk] ereg_replace() behavior

Anirudh Zala arzala at gmail.com
Thu Jul 10 21:51:57 EDT 2008


On Thursday 10 Jul 2008 19:36:15 Jake McGraw wrote:
> > echo ereg_replace('__([:alnum:]{1})',strtoupper('\1'),'ss__name');
>
> Problem:
>
> strtoupper('\1') = '\1' => 'ssname'
>
> Problem:
>
> str_repeat('\1',5) = '\1\1\1\1\1' => 'ssnnnnname'
>
> Solution:
>
> preg_replace_callback('/__([a-z])/i', create_function('$a', 'return
> strtoupper($a);'), 'ss__name');
>
> Check out
>
> PCRE (Perl Compatible Regular Expressions)
> http://us3.php.net/manual/en/ref.pcre.php
> preg_replace_callback http://us3.php.net/preg_replace_callback
> create_function http://us3.php.net/create_function

Thanks to all. Later found that it is limitation of "ereg_replace()" (against 
preg_replace()) that it can't execute replacement argument as PHP code nor 
can call other call_back functions like we could do with preg_replace.

Anirudh Zala

>
> - jake
>
> > Outputs:
> >
> > "ssname" instead of "ssName"
> >
> > Here is bit matching explanation of this behavior
> > http://bugs.php.net/bug.php?id=24645&edit=1 (2nd last answer)
> >
> > Contrary to above explanation, following code works properly (I just used
> > different function instead of "strtoupper"):
> >
> > echo ereg_replace('__([:alnum:]{1})',str_repeat('\1',5),'ss__name');
> >
> > Outputs:
> >
> > "ssnnnnname" as expected.
> >
> > Any idea of such behavior? Or am i making mistake somewhere? I am using
> > PHP 5.1.6 on FC6. Although there is solution in PCRE (preg_replace()) and
> > by many other, long, ways, I would like to dig first about this behavior.
> >
> > Thanks
> >
> > Anirudh Zala
> > _______________________________________________
> > New York PHP Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > NYPHPCon 2006 Presentations Online
> > http://www.nyphpcon.com
> >
> > Show Your Participation in New York PHP
> > http://www.nyphp.org/show_participation.php
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php





More information about the talk mailing list