【问题标题】:Normalize Name-Surname strings: PHP+REGEX (Spanish chars- UTF8)规范化姓名-姓氏字符串:PHP+REGEX(西班牙语字符 - UTF8)
【发布时间】: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 等。但不能使其与“特殊”西班牙字符一起正常工作。

有什么建议吗?

【问题讨论】:

    标签: php regex utf-8


    【解决方案1】:

    看不到,你在哪里使用Multibyte String Functions

    也许这会方便您的需求:

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

    output

    Héctor Mañaç
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 是的,我的文件也是用UTF8编码的,结果是:HÉctor MaÑaÇ
      • 我用同样的方法测试了你的代码,它工作正常。得到这个“Héctor Mañaç”。
      • 请将编码设为“ANSI”再试一次
      • 文件在没有BUM的情况下重新转换为ANSI、UTF8和UTF8,结果相同,可能是与服务器配置有关的问题?
      猜你喜欢
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多