函数在PHP中删除重音符号和其他字符的问题 [英] Problem with function removing accents and other characters in PHP

查看:132
本文介绍了函数在PHP中删除重音符号和其他字符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个简单的函数,可以从字符串中删除一些不需要的字符.

I found a simple function to remove some undesired characters from a string.

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;
}

当我将其用于重音符号或其他字符时,例如áéñí"一词,它会打印出那些问号或奇怪的字符,例如: 输出http://img217.imageshack.us/img217/6794/59472278.jpg

When I use it on accents or other characters, like this word 'á é ñ í' it prints out those question marks or weird characters, like: output http://img217.imageshack.us/img217/6794/59472278.jpg

注意:我正在UTF-8中使用strclean.php(包含此函数)和index.php. index.php如下所示:

Note: I'm using strclean.php (which contains this function) and index.php, both in UTF-8. index.php looks as follows:

<!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>

我在做什么错了?

推荐答案

我已经测试了您的代码,并且strtolower函数出错...

I have tested your code, and error is in strtolower function...

用mb_strtolower代替它,例如波纹管

Replace it with mb_strtolower, like bellow

<!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>

这篇关于函数在PHP中删除重音符号和其他字符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆