NYCPHP Meetup

NYPHP.org

[nycphp-talk] ereg_replace() behavior

Anirudh Zala arzala at gmail.com
Thu Jul 10 09:54:35 EDT 2008


I am facing a strange behavior of PHP's ereg_replace() function. Here is 
scenario:

Problem:

Removing "__" (2 continuous underscores) and Capitalizing character that 
immediately follows "__" from a string. In our example our string 
is 'ss__name' and expected output should be 'ssName'.

Solution:

Tried following code using function "ereg_replace()" but character is not 
getting capitalized:

echo ereg_replace('__([:alnum:]{1})',strtoupper('\1'),'ss__name');

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



More information about the talk mailing list