I am having small problem. I am trying to replace words in sentence ie
HELLO World WHAT
by
Hello World What
so far have way to detecting it:
preg_replace('/(\b[A-Z][A-Z]+\b)/sm','$1', $string);
but it does nothing as I can't put as an argument ucwords('$1').
Any help would be great.
cheers, /Marcin
PS this kind of methods are not good: ucwords(strtolower($string)); as I want to leave all what wasn't all caps as it was.
How about using:
ucwords(strtolower($string));
You can combine this approach with the e
modifier:
preg_replace('/(\b[A-Z][A-Z]+\b)/sme','ucwords(strtolower($1))', $string);
It is slightly more efficient to use an anonymous function rather then call on the /e
modifier.
$formatted = preg_replace_callback(
'/(\b[A-Z][A-Z]+\b)/',
create_function(
'$matches',
'return ucwords(strtolower($matches[0]));'
),
$string
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.