I was working on a simple accent stripping function on PHP
$your_data = $_POST["input"];
function stripchar($str) {
$char = array(
'a'=>'á|à|ặ|â',
);
foreach($char as $stripped_char => $original_char){
$str = preg_replace("/($original_char)/i", $stripped_char, $str);
}
return $str;
}
echo stripchar($your_data);
It worked well if I try to echo accented characters (á à ặ â), of which it would return the intended characters (a a a a).
However, when I put into use - taking a string variable from $string = $_POST["input"] from the html file, inputted by the user, the function did not change the accented characters at all.
The html code:
<!DOCTYPE html>
<html>
<body>
<div class="inputbox">
<form action="action_page.php" method="post">
<textarea type="text" name="input" style="width: 100%; height: 200px;"></textarea>
<input type="submit" name="action" value="Strip" class="button_form">
</form>
</div>
</body>
</html>
I am still quite confused. Tried var_dump the $string variable and it showed up as string variable. I also tested another function that takes accented string in the script again, and it still worked. But not from the string from the input field!
答案 0 :(得分:2)
请改为尝试:
function stripAccents($stripAccents){
return strtr($stripAccents,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ','aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}
答案 1 :(得分:0)
我终于找到了答案 - 结果问题不在于服务器端脚本,而是来自客户端 - 特别是来自我的html文件。 html表单只是发送破碎的重音字符串,因此我的php脚本无法按预期处理字符串。
所以我只需在html文件中添加此行<meta charset="UTF-8">
,现在一切正常。