我找到了一个简单的函数来从字符串中删除一些不需要的字符。
function strClean($input){
$input = strtolower($input);
$b = array("á","é","í","ó","ú", "ñ", " "); //etc...
$c = array("a","e","i","o","u","n", "-"); //etc...
$input = str_replace($b, $c, $input);
return $input;
}
当我在口音或其他角色上使用它时,比如这个词'áéñí'它会打印出那些问号或奇怪的字符,例如: output http://img217.imageshack.us/img217/6794/59472278.jpg
注意:我正在使用strclean.php(包含此函数)和index.php,两者都是UTF-8。 index.php如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
include('strclean.php');
echo 'óóóáà';
echo strClean('óóóáà');
?>
</body>
</html>
我做错了什么?
答案 0 :(得分:5)
使用
iconv('UTF-8', 'ASCII//TRANSLIT', $input);
答案 1 :(得分:4)
您可以尝试iconv。
答案 2 :(得分:3)
是否会发生替换,即在预先打印$ input时是否会得到相同的奇怪字符?如果是这样,PHP源代码文件的字符集和输入不匹配,您可能需要在替换之前在输入上使用iconv()。
编辑:我将您的两个文件上传到我的网络服务器,打印和清洁工作正常(请参阅http://www.tag-am-meer.com/test1/)。这是在PHP 4.4.9和Firefox 3.0.6上。我想到了更多潜在的问题:
答案 3 :(得分:2)
我已经测试了你的代码,错误是strtolower函数......
将其替换为mb_strtolower,如下文
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?php
function strClean($input) {
$input = mb_strtolower($input, 'UTF-8');
$b = array("á","é","í","ó","ú", "n", " ");
$c = array("a","e","i","o","u","n", "-");
return str_replace($b, $c, $input);
}
$string = 'á é í ó ú n abcdef ghij';
echo $string ."<br />". strClean($string);
?>
</body>
</html>
答案 4 :(得分:0)
为什么要删除重音?您是否可能只想忽略它们?如果是这样,this answer有一个Perl解决方案,演示如何执行此操作。请注意,Perl是外语。 :)
答案 5 :(得分:0)
我之前发现自己遇到了这个麻烦,我试图跟随这篇文章的主角以及我在途中找到的其他人,并且没有简单的解决方案,因为你必须知道你的系统使用的字符集(在我的情况下) ISO-8859-1)这就是我所做的:
function quit_accenture($str){
$pattern = array();
$pattern[0] = '/[Á|Â|À|Å|Ä]/';
$pattern[1] = '/[É|Ê|È]/';
$pattern[2] = '/[Í|Î|Ì|Ï]/';
$pattern[3] = '/[Ó|Ô|Ò|Ö]/';
$pattern[4] = '/[Ú|Û|Ù|Ü]/';
$pattern[5] = '/[á|â|à|å|ä]/';
$pattern[6] = '/[ð|é|ê|è|ë]/';
$pattern[7] = '/[í|î|ì|ï]/';
$pattern[8] = '/[ó|ô|ò|ø|õ|ö]/';
$pattern[9] = '/[ú|û|ù|ü]/';
$replacement = array();
$replacement[0] = 'A';
$replacement[1] = 'E';
$replacement[2] = 'I';
$replacement[3] = 'O';
$replacement[4] = 'U';
$replacement[5] = 'a';
$replacement[6] = 'e';
$replacement[7] = 'i';
$replacement[8] = 'o';
$replacement[9] = 'u';
return preg_replace($pattern, $replacement, $str);
}
$txt = $_POST['your_htmled_text'];
//Convert to your system's charset. I checked this on the php.ini
$txt = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $txt);
//Apply your function
$txt = quit_accenture($txt);
//output
print_r($txt);
这对我有用,但我也认为是正确的方法:)