note 86871 deleted from function.ucwords by cmb
| From: | cmb@php.net | Date: | Sun, 24 Nov 2019 09:08:17 +0000 |
| Subject: | note 86871 deleted from function.ucwords by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213235@lists.php.net to get a copy of this message | ||
Note Submitter: dyer85 at gmail dot com
----
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);
}
?>