note 86871 added to function.ucwords

From: Date: Fri, 07 Nov 2008 11:33:16 +0000
Subject: note 86871 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-146275@lists.php.net to get a copy of this message
When attempting to adopt a solution similar to Catalin's post (20-Oct-2008 10:14), I ran into some additional problems. Just a heads up, Catalin's wasn't capitalizing correctly when a name like "O'reilley" appeared at the start of the string or a new line. Also, it doesn't account for locale-sensitivity. I also needed to recognize additional Irish surnames, such as "McArthur/MacArthur" or "FitzGerald". I also didn't want to misinterpret French as an Irish surname, so, "S'il vous plaît" shouldn't result in the "i" in "S'il" capitalized. I modified Catalin's version, but it's still not perfect. This version happened to suit my needs, so be sure to assess your own needs before using. <?php function my_ucwords($str) { $str = ucwords($str); // Not perfect return preg_replace( '/ (?: ^ | \\b ) # assertion: beginning of string or a word boundary ( O\' | Ma?c | Fitz) # attempt to match Irish surnames ( [^\W\d_] ) # match next char; we exclude digits and _ from \w /xe', "'\$1' . strtoupper('\$2')", $str); } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 96.240.47.235 ---- Manual Page -- http://www.php.net/manual/en/function.ucwords.php Edit -- https://master.php.net/note/edit/86871 Del: integrated -- https://master.php.net/note/delete/86871/integrated Del: useless -- https://master.php.net/note/delete/86871/useless Del: bad code -- https://master.php.net/note/delete/86871/bad+code Del: spam -- https://master.php.net/note/delete/86871/spam Del: non-english -- https://master.php.net/note/delete/86871/non-english Del: in docs -- https://master.php.net/note/delete/86871/in+docs Del: other reasons-- https://master.php.net/note/delete/86871 Reject -- https://master.php.net/note/reject/86871 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146275) next »