Re: preg_replace \\1 yIKES!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux