3v4l.org

run code in 300+ PHP versions simultaneously
<?php function smart_uc_words($string) { $string = strtolower(trim($string)); // Capitalise any word char preceded by a non-word char other than an apostrophe $string = preg_replace_callback('/(?<!\w|\')(\w)/', function($m){ return strtoupper($m[1]); }, $string); // Capitalise any word char which comes between an apostrophe and another word char $string = preg_replace_callback('/(?<=\')(\w)(?=\w)/', function($m){ return strtoupper($m[1]); }, $string); return $string; } echo smart_uc_words("O'BRIEN'S BAR NO.FOUR");

preferences:
57.66 ms | 405 KiB | 6 Q