function lower_tail($str) {
return $str[0].strtolower(substr($str, 1));
}
$sentence = "tHIs is the StacKOverFlOW SiTE";
$new_sentence = implode(' ', array_map('lower_tail', explode(' ', $sentence)));
アップデート:
他のいくつかの状況を処理するより良いバージョンを次に示します。
$sentence = "Is tHIs, the StacKOverFlOW SiTE?\n(I doN'T know) [A.C.R.O.N.Y.M] 3AM";
$new_sentence = preg_replace_callback(
"/(?<=\b\w)(['\w]+)/",
function($matches) { return strtolower($matches[1]); },
$sentence);
echo $new_sentence;
// Is this, the Stackoverflow Site?
// (I don't know) [A.C.R.O.N.Y.M] 3am
// OUTPUT OF OLD VERSION:
// Is this, the Stackoverflow Site?
// (i don't know) [a.c.r.o.n.y.m] 3am
(注: PHP 5.3+)