Re: preg_replace \\1 yIKES!
| From: | Rafael | Date: | Tue, 13 Jun 2006 23:13:10 +0000 |
| Subject: | Re: preg_replace \\1 yIKES! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237938@lists.php.net to get a copy of this message | ||
Capitalize the first letter of a word: ucwords()[1]
Now, if it's just for practice, then you need a little change in you code, since strtoupper('\\2') will give '\\2' (i.e. does nothing) and the letter will remain the same. One way to do it would be
$text = 'hello world (any ...world)';
echo preg_replace('/\b(\w)/Xe', 'strtoupper("\\1")', $text);
check the manual[2] for more info...
[1] http://php.net/ucwords
[2] http://php.net/manual/en/reference.pcre.pattern.syntax.php
http://php.net/manual/en/reference.pcre.pattern.modifiers.phpsam 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! -- Atentamente / Sincerely, J. Rafael Salazar Magaña