Php 为什么仅仅因为它来自$\u POST,它就不起作用?

Php 为什么仅仅因为它来自$\u POST,它就不起作用?,php,Php,我在前面做了一个函数来遍历给定的字符串,并用e、o替换一些选定的字符,如é、ó等,以去除外来字符 当我设置字符串并像这样使用它时,该函数工作正常: $string = "Héllo"; echo replace_fc($string); 返回:你好 但是,当我从输入框中输入Héllo的post数据时,如下所示: $string = $_POST['words']; echo replace_fc($string); 返回HA�洛 它不起作用 有人知道为什么会这样吗?如果我回显POST数据,它

我在前面做了一个函数来遍历给定的字符串,并用e、o替换一些选定的字符,如é、ó等,以去除外来字符

当我设置字符串并像这样使用它时,该函数工作正常:

$string = "Héllo";
echo replace_fc($string);
返回:你好

但是,当我从输入框中输入Héllo的post数据时,如下所示:

$string = $_POST['words'];
echo replace_fc($string);
返回HA�洛

它不起作用

有人知道为什么会这样吗?如果我回显POST数据,它与我输入的数据完全一样,Héllo,它在页面上显示得很好。这个函数不应该输出任何特殊字符,只是Hello,所以我不明白为什么它会出错。函数如下所示。顺便说一下,忽略我的字符串/数组系统,我这么做是因为我知道它不是最优的,我只是为了好玩才这么做的

function replace_fc($chars){
    //Hexadecimal characters separated by commas
    $hexes_str = "
        C0-A,C1-A,C2-A,C3-A,C4-A,C5-A,C6-A,                             "/* Capital A       */."
        E0-a,E1-a,E2-a,E3-a,E4-a,E5-a,E6-a,                             "/* Lowercase A     */."
        C8-E,C9-E,CA-E,CB-E,                                            "/* Capital E       */."
        E8-e,E9-e,EA-e,EB-e,                                            "/* Lowercase E     */."
        CC-I,CD-I,CE-I,CF-I,                                            "/* Capital I       */."
        EC-i,ED-i,EE-i,EF-i,                                            "/* Lowercase I     */."
        D2-O,D3-O,D4-O,D5-O,D6-O,D8-O,                                  "/* Capital O       */."
        F2-o,F3-o,F4-o,F5-o,F6-o,F8-o,                                  "/* Lowercase O     */."
        D9-U,DA-U,DB-U,DC-U,                                            "/* Capital U       */."
        F9-u,FA-u,FB-u,FC-u,                                            "/* Lowercase U     */."
        C7-C,D0-D,D1-N,D7-X,DD-Y,DF-B,DF-S,                             "/* Capital Misc    */."
        E7-c,F0-d,F1-n,D7-x,FD-y,FE-b,FF-y
    ";

    //Set them in to an array
    $hexes = explode(",", $hexes_str);
    //Remove spaces
    $hexes = array_map("trim", $hexes);

    //Empty array for all of my characters
    $numhtml = array();

    //For each of the numbers
    foreach($hexes as $x){
        //If $x is set to something (might be empty because of above explode, best to be safe)
        if($x){
            //Split them up
            $temp = explode("-",$x);
            //Stick them in the array
            $numhtml[$temp[1]][] = $temp[0];
        }
    }

    //Split the chars
    $chars_split = str_split($chars);

    //Set up empty vars to use
    $chars_enc = array(); $chars_result = null;

    //Get the urlencode versions
    foreach($chars_split as $x){
        $chars_enc[] = urlencode($x);
    }

    //For each of those
    foreach($chars_enc as $x){
        //If it has a % in it
        if(strpos($x, "%") !== false){
            //Get a version without the %
            $char = str_replace("%", "", $x);
            $replaced = false;

            //Go through the numhtml array
            foreach($numhtml as $key => $y){
                //If it has this char in it
                if(in_array($char, $y)){
                    //Add the key to the result
                    $chars_result .= $key;

                    //Set replaced to true
                    $replaced = true;
                }
            }

            //If replaced is still false
            if(!$replaced){
                //Add the original char
                $chars_result .= urldecode($x);
            }
        //If it is a space
        }elseif($x == "+"){
            $chars_result .= " ";
        //If there is no %, and it's not a space
        }else{
            //Then add it normally
            $chars_result .= $x;
        }
    }

    //Return the result
    return $chars_result;
}

有人在这里发布了一个答案,帮助我解决了这个问题,但它已被删除,所以我将解释我对此所说的和所做的

答案告诉我$_POST自动编码数据,我以前从未见过这种情况,因为直到今天我决定涉猎这一领域之前,我从未处理过UTF-8和外来字符

我解决这个问题的方法是:

$string = utf8_decode($_POST['words']);
$string = replace_fc($string); 
echo "$string<br />";
utf8_decode是修正POST数据的函数,以便我可以使用它


现在已解决,感谢您发布答案的人。

这是一个字符集问题。您的输入字段是以另一种方式解码的,而不是您正在编写代码的文件。我正要+1答案,因为它帮助我解决了这个问题,但谢谢您,我现在已经设法解决了这个问题。我将发布一个答案,以表明它已经完成。