すべての単語を大文字にしてから、 a、of、. 最初と最後の単語は大文字のままにする必要があります。\b の代わりに使用してみましたが、奇妙な問題が発生しました。私も試しましたが、それは「文字列の終わりではない」という意味ではないようです\s
[^$]
function titleize($string){
return ucfirst(
preg_replace("/\b(A|Of|An|At|The|With|In|To|And|But|Is|For)\b/uie",
"strtolower('$1')",
ucwords($string))
);
}
これは、私が修正しようとしている唯一の失敗したテストです。最後の「in」は大文字のままにする必要があります。
titleize("gotta give up, gotta give in");
//Gotta Give Up, Gotta Give In
これらのテストはパスします:
titleize('if i told you this was killing me, would you stop?');
//If I Told You This Was Killing Me, Would You Stop?
titleize("we're at the top of the world (to the simple two)");
//We're at the Top of the World (to the Simple Two)
titleize("and keep reaching for those stars");
//And Keep Reaching for Those Stars