名称大写的规则可能相当复杂。这是我如何处理这些问题的教学示例。请参阅http://www.laprbass.com/RAY_capitalize_names.php
<?php // RAY_capitalize_names.php
error_reporting(E_ALL);
// SOME TEST NAMES
$names = array
( "o'brien"
, 'MCAFEE'
, "barrett-o'reilly"
, "smith jones"
, "burns"
, "CROWTHER"
, "George w. bush, iiI"
, "RONALD MCDONALD"
, "RONALD MCDONALD-o'brien"
, "van De Graaff GeneratoR"
)
;
// TEST EACH CASE
foreach ($names as $name)
{
echo "<br/>$name ";
echo fixname($name);
}
// FUNCTION TO HANDLE NAMES
function fixname($name)
{
// SPECIAL CASES FOR UPPER OR LOWER CASE DISPOSITION
$uc = array // UPPERCASE AFTER ANY OF THESE
( 'Mc'
, "'"
, '-'
)
;
$lc = array // ALWAYS LOWER CASE
( 'Van De '
)
;
$mc = array // ALWAYS UPPER CASE
( 'Iii'
)
;
// REMOVE UNNECESSARY BLANKS
$name = preg_replace('/\s\s+/', ' ', $name);
// START WITH LOWER CASE AND UPPER FIRST
$name = strtolower($name);
$name = ucwords($name);
// CHECK FOR KNOWN SPECIAL UPPER-CASES
foreach ($uc as $dlm)
{
// FIX THE Mcdonald EXAMPLE, ETC
$namex = explode($dlm, $name);
foreach ($namex as $k => $v)
{
$namex[$k] = ucwords($v);
}
$name = implode($dlm, $namex);
}
// CHECK FOR KNOWN CONSTANT LOWER-CASES
foreach ($lc as $dlm)
{
// FIX THE van de Graaff EXAMPLE
$name = str_replace($dlm, strtolower($dlm), $name);
}
// CHECK FOR KNOW CONSTANT UPPERCASE
foreach ($mc as $dlm)
{
// FIX THE Bush, III EXAMPLE
$name = str_replace($dlm, strtoupper($dlm), $name);
}
// RETURN THE REPAIRED STRING
return $name;
}