NYCPHP Meetup

NYPHP.org

[nycphp-talk] ereg_replace() behavior

Jake McGraw jmcgraw1 at gmail.com
Thu Jul 10 10:06:15 EDT 2008


> 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

- 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
>



More information about the talk mailing list