规范名称 - 姓氏字符串:PHP + REGEX(西班牙语字符 - UTF8)

时间:2014-02-07 09:38:36

标签: php regex utf-8

我有名字和姓氏的字符串,我需要用函数进行规范化,并使它们像:

姓名姓氏(我可以收听NAME SURNAME,姓名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等。但不能使它与“特殊”西班牙字符一起使用。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

无法看到您使用Multibyte String Functions的位置。

也许这对您的需求很方便:

echo mb_convert_case("HÉCTOR MAÑAÇ", MB_CASE_TITLE, "UTF-8");

output

Héctor Mañaç

答案 1 :(得分:0)

您的功能也适用于给定的示例。请检查您的文件编码类型。它必须是UTF-8。你可以在Notepadd ++中查看它。