如何去掉字符串中的非英文字母?
我想用字符串中的英语字符删除土耳其语字符,这就是我编写的函数。但是,在服务器中,它无法识别这些字母。有什么建议吗?
function strtolower_tr($string)
{
$low=array("Ü" => "U","ü" => "u", "Ö" => "O","ö" => "o", "Ğ" => "G","ğ" => "g", "Ş" => "S","ş" => "s", "Ç" => "C","ç" => "c", "İ" => "I","i" => "i", "I" => "i","I" => "I");
return strtolower(strtr($string,$low));
}
--------在服务器中--------
$low=array("?~\" => "u?", "?~V" => "o?", "?~^" => "g", "?~^" => "s", "?~G" => "c", "İ" => "i", "I" => i");
编辑:
我发现了这个:但是,它不适用于字母“ı”和“I” setlocale(LC_ALL, 'en_US.UTF8'); 函数clearUTF($s) { $r = ''; $s1 = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); $j = 0; for ($i = 0; $i < strlen($s1); $i++) { $ch1 = $s1[$i]; $ch2 = @mb_substr($s, $j++, 1, 'UTF-8'); if (strstr('`^~\'"', $ch1) !== false) { if ($ch1 <> $ch2) { --$j; 继续; } } $r .= ($ch1=='?') ? $ch2 : $ch1; } 返回$r; }
I want to get rid of the turkish characters with the english ones in the string and thats the function I wrote. However, in the server, its not recognized the letters. Any suggestions?
function strtolower_tr($string)
{
$low=array("Ü" => "U","ü" => "u", "Ö" => "O","ö" => "o", "Ğ" => "G","ğ" => "g", "Ş" => "S","ş" => "s", "Ç" => "C","ç" => "c", "İ" => "I","i" => "i", "I" => "i","I" => "I");
return strtolower(strtr($string,$low));
}
-------IN THE SERVER-------
$low=array("?~\" => "u?", "?~V" => "o?", "?~^" => "g", "?~^" => "s", "?~G" => "c", "İ" => "i", "I" => i");
EDITED:
I found this: However, its not working for letter 'ı' and 'I'
setlocale(LC_ALL, 'en_US.UTF8');
function clearUTF($s)
{
$r = '';
$s1 = @iconv('UTF-8', 'ASCII//TRANSLIT', $s);
$j = 0;
for ($i = 0; $i < strlen($s1); $i++) {
$ch1 = $s1[$i];
$ch2 = @mb_substr($s, $j++, 1, 'UTF-8');
if (strstr('`^~\'"', $ch1) !== false) {
if ($ch1 <> $ch2) {
--$j;
continue;
}
}
$r .= ($ch1=='?') ? $ch2 : $ch1;
}
return $r;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的文件是utf编码的吗?您应该对源文件使用 utf 编码,这将解决您的问题。
Is you file in utf encoding? You should use utf encoding for your source files, this will solve your problem.