如何去掉字符串中的非英文字母?

发布于 2024-11-06 10:10:19 字数 1097 浏览 0 评论 0原文

我想用字符串中的英语字符删除土耳其语字符,这就是我编写的函数。但是,在服务器中,它无法识别这些字母。有什么建议吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-11-13 10:10:19

你的文件是utf编码的吗?您应该对源文件使用 utf 编码,这将解决您的问题。

Is you file in utf encoding? You should use utf encoding for your source files, this will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文