我之前创建了一个函数来遍历给定的字符串,并用e,o替换选择的几个字符,如é,ó等,以删除外来字符。
当我设置一个字符串并使用它时,该函数正常工作:
$string = $_POST['words'];
echo replace_fc($string);
返回:Hello
然而,当我从我输入Héllo的输入框中获取发布数据时,如下所示:
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;
}
返回HA llo
它不起作用。
有人知道为什么会这样吗?如果我回显POST数据,就像我输入它(Héllo)一样,它在页面上显示就好了。该功能不应该输出任何特殊字符,只是“你好”,所以我不明白为什么它搞乱了。功能如下。 (P.S.忽略我的字符串/数组系统,我知道它不是最优的,我只是为了这些功能而做了。)
orderDate
答案 0 :(得分:0)
有人在这里发布了一个答案,帮助我解决了这个问题,但它已被删除,所以我将解释所说的内容以及我对此做了什么。
答案告诉我,$ _POST会自动编码数据,而我之前从未见过这个数据,因为在我决定今天涉嫌之前,我从未处理过UTF-8和外国字符。
我解决这个问题的方法是做以下事情:
$string = utf8_decode($_POST['words']);
$string = replace_fc($string);
echo "$string<br />";
utf8_decode()是纠正POST数据的功能,以便我可以使用它。
现在解决了,谢谢你发布答案的任何人。