php - Making first character of all sentences upper case -
i have function suppose make first character of sentences uppercase, reason, it's not doing first character of first sentence. why happening, , how fix it?
<?php function ucall($str) { $str = preg_replace_callback('/([.!?])\s*(\w)/', create_function('$matches', 'return strtoupper($matches[0]);'), $str); return $str; } //end of function ucall($str) $str = ucall("first.second.third"); echo $str; ?>
result:
first.second.third
expected result:
first.second.third
it not uppercase first word because regular expression requires there 1 of .
, !
or ?
in-front of it. first word not have 1 of characters in-front of it.
this it:
function ucall($str) { return preg_replace_callback('/(?<=^|[\.\?!])[^\.]/', function ($match) { return strtoupper($match[0]); }, $str); }
it uses positive look-behind make sure 1 of .
, !
, ?
, or start of line, in-front of matched string.
Comments
Post a Comment