【发布时间】:2014-03-04 15:34:37
【问题描述】:
我有带有姓名和姓氏的字符串,我需要使用函数对其进行规范化,并使它们像:
姓氏(我可以接收像NAME SURNAME、Name SURNAME 等字符串)
我找到了这个snipet:
echo nameize("HÉCTOR MAÑAÇ");
function nameize($str,$a_char = array("'","-"," ")){
//$str contains the complete raw name string
//$a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default.
$string = strtolower($str);
foreach ($a_char as $temp){
$pos = strpos($string,$temp);
if ($pos){
//we are in the loop because we found one of the special characters in the array, so lets split it up into chunks and capitalize each one.
$mend = '';
$a_split = explode($temp,$string);
foreach ($a_split as $temp2){
//capitalize each portion of the string which was separated at a special character
$mend .= ucfirst($temp2).$temp;
}
$string = substr($mend,0,-1);
}
}
return ucfirst($string);
}
效果很好,但是,正如您所看到的测试这个确切的例子,不解析西班牙字符(utf8)我已经测试过 mb_regex_encoding("UTF-8"); mb_internal_encoding("UTF-8");,标头 UTF8 等。但不能使其与“特殊”西班牙字符一起正常工作。
有什么建议吗?
【问题讨论】: