note 125209 added to function.ucwords

From: Date: Sat, 25 Jul 2020 17:08:31 +0000
Subject: note 125209 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-214892@lists.php.net to get a copy of this message
Based on code sample originally posted by "Anonymous" on 2005-12-14. The /e modifier is no longer supported by preg_replace(). Rewritten to use preg_replace_callback() instead. Tested with PHP 7.3. The function below will standardize the capitalization on people's names, addresses, and the titles of reports and essays . Adapt the lists in "$all_uppercase" and "$all_lowercase" to suit the data that you are working with. function ucwords_title($string, $is_name = false) { // Exceptions to standard case conversion if ($is_name) { $all_uppercase = ''; $all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And'; } else { // Addresses, essay titles ... and anything else $all_uppercase = 'Us|Ca|Mx|Po|Rr|Se|Sw|Ne|Nw'; $all_lowercase = 'A|And|As|By|In|Of|Or|To'; } $prefixes = 'Mac|Mc'; $suffixes = "'S"; // Trim whitespace and convert to lowercase $str = strtolower(trim($string)); // Capitalize all first letters $str = preg_replace_callback('/\\b(\\w)/', function ($m) { return strtoupper($m[1]); }, $str); if ($all_uppercase) { // Capitalize acronyms and initialisms (e.g. PHP) $str = preg_replace_callback('/\\b(' . $all_uppercase . ')\\b/', function ($m) { return strtoupper($m[1]); }, $str); } if ($all_lowercase) { // Decapitalize short words (e.g. and) if ($is_name) { // All occurrences will be changed to lowercase $str = preg_replace_callback('/\\b(' . $all_lowercase . ')\\b/', function ($m) { return strtolower($m[1]); }, $str); } else { // First and last word will not be changed to lower case (i.e. titles) $str = preg_replace_callback('/(?<=\\W)(' . $all_lowercase . ')(?=\\W)/', function ($m) { return strtolower($m[1]); }, $str); } } if ($prefixes) { // Capitalize letter after certain name prefixes (e.g 'Mc') $str = preg_replace_callback('/\\b(' . $prefixes . ')(\\w)/', function ($m) { return $m[1] . strtoupper($m[2]); }, $str); } if ($suffixes) { // Decapitalize certain word suffixes (e.g. 's) $str = preg_replace_callback('/(\\w)(' . $suffixes . ')\\b/', function ($m) { return $m[1] . strtolower($m[2]); }, $str); } return $str; } // A name example print ucwords_title("MARIE-LOU VAN DER PLANCK-ST.JOHN", true); // Output: Marie-Lou van der Planc-St.John // A title example print ucwords_title("to be or not to be"); // Output: "To Be or Not to Be" ---- Server IP: 45.112.84.4 Probable Submitter: 103.51.166.36 (proxied: 34.198.4.123) ---- Manual Page -- http://php.net/manual/en/function.ucwords.php Edit -- https://master.php.net/note/edit/125209 Del: integrated -- https://master.php.net/note/delete/125209/integrated Del: useless -- https://master.php.net/note/delete/125209/useless Del: bad code -- https://master.php.net/note/delete/125209/bad+code Del: spam -- https://master.php.net/note/delete/125209/spam Del: non-english -- https://master.php.net/note/delete/125209/non-english Del: in docs -- https://master.php.net/note/delete/125209/in+docs Del: other reasons-- https://master.php.net/note/delete/125209 Reject -- https://master.php.net/note/reject/125209 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214892) next »