Convert wholly capitalized words in a string into uppercase first only

Hi all,

I have a string in which I want to find any wholly uppercase word (e.g. TEST ) and then convert it into ( Test ) using php .

I can’t code it , headache after many trials !

Can someone help ?

Hint : I can’t use explode(" ",$string ) to convert it into an array to itreate it … Because the string words are not only separated by spaces , but some words are separated by \

Regards,
Dr Mostafa

Try ucwords, example from PHP manual


<?php
$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

You will need to use preg_replace_callback.

Something like this (warning: hasn’t been tested).


$string = "The quick BROWN fox jumped over the LAZY dog.";


$new_string = preg_replace_callback('~[A-Z]{2,}~', create_function('$m', 'return ucwords(strtolower($m[0]));'), $string);