欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP中trim 会导致中文乱码的解决方案

程序员文章站 2022-05-30 23:42:52
...
function mb_rtrim($string, $trim, $encoding)
{
 
    $mask = [];
    $trimLength = mb_strlen($trim, $encoding);
    for ($i = 0; $i < $trimLength; $i++) {
        $item = mb_substr($trim, $i, 1, $encoding);
        $mask[] = $item;
    }
 
    $len = mb_strlen($string, $encoding);
    if ($len > 0) {
        $i = $len - 1;
        do {
            $item = mb_substr($string, $i, 1, $encoding);
            if (in_array($item, $mask)) {
                $len--;
            } else {
                break;
            }
        } while ($i-- != 0);
    }
 
    return mb_substr($string, 0, $len, $encoding);
}
 
mb_internal_encoding("UTF-8");
$tag = "互联网产品、";
$encoding = mb_internal_encoding();
print_r(mb_rtrim($tag, "、",$encoding));

 

转载于:https://my.oschina.net/koloor/blog/1823319