NYCPHP Meetup

NYPHP.org

[nycphp-talk] ereg_replace() behavior

Dan Cech dcech at phpwerx.net
Thu Jul 10 10:18:45 EDT 2008


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

Beat me to it, preg_replace_callback is probably better than /e from a 
readability and maintenance perspective too!

Dan



More information about the talk mailing list