Re: preg_replace \\1 yIKES!

From: Date: Tue, 13 Jun 2006 18:56:17 +0000
Subject: Re: preg_replace \\1 yIKES!
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-237915@lists.php.net to get a copy of this message
sam wrote: > > Wow this is hard I can't wait till I get the hang of it. > > Capitalize the first letter of a word. > > echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', > 'yikes!'); > // outputs yikes! > > Nope didn't work. > > So I want to see if I'm in the right place: > > echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . > strtoupper('\\2') . '-' > . '\\3', 'yikes!'); > //outputs -y-ikes! > > So yea I've got it surrounded why now strtoupper: Yikes! to running strtoupper() on the string literal '\\2' which after it has been capitalized will be '\\2'. if you *really* want to use preg_replace(): echo preg_replace("/(^)(.)(.*\$)/e", "\"\\1\" . strtoupper(\"\\2\") . \"\\3\"", "yikes!"), "\n"; notice the 'e' modifier on the regexp - it causes the replace string to be treated as a string of php code that is autoamtically evaluated - checvk the manual for more info. (I used double quotes only so that I could test the code on the cmdline using 'php -r') ...BUT I suggest you try this function instead: ucfirst() > > Thanks > > --PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#237915) next »