With regex.
If you intend to work with Latin letters without an accent, it can be as simple as
$str = 'he said "hello WORLD"';
echo preg_replace('/\b([a-z])/e', 'strtoupper(\'$1\')', strtolower($str));
This matches any lowercase Latin letter that is preceded by a word boundary . The letter is replaced with its upper case.
If you want this to work with other languages and scripts, you will need to pretend:
$str = 'he said "καλημέρα ΚΌΣΜΕ"'; // this has to be in UTF-8
echo preg_replace('/(?<!\p{L})(\p{Ll})/eu',
'mb_convert_case(\'$1\', MB_CASE_UPPER, \'UTF-8\')',
mb_convert_case($str, MB_CASE_LOWER, 'UTF-8'));
, Unicode PCRE , u preg_replace. , ( \p{Ll}), , - (negative lookbehind \p{L}). .
.
: , .
(?<=\s|^)([a-z])
(?<=\s|^)(\p{Ll})