Centos下安装php的mbstring扩展

php的mbstring扩展如果没有安装会导致一些问题,例如登陆phpMyAdmin的时候会提示没字符串编码和字符串处理库php_mbstring,有些程序中会用到mb_substr函数没有php的mbstring扩展当这些程序运行的时候通常会提示“Fatal error: Call to undefined function mb_substr()”,对于第二个问题可以用另一篇文章“mb_substr修正函数”解决。


但是如果你有自己的服务器或者vps的权限还是安装php的mbstring扩展好,下面是安装步骤,很简单:
SSH连接之后

yum -y install php-mbstring

然后编辑你的php.ini文件添加

extension=mbstring.so

最后重启httpd服务

service httpd restart

完成。

mb_substr修正函数

不支持mb_substr函数的环境中使用了mb_substr函数会出现“Fatal error: Call to undefined function mb_substr()”的提示,下面的函数是定义的替代mb_substr的函数,作用是一样的,用于不支持mb_substr的地方。

// Patch in multibyte support
if (!function_exists('mb_substr')) {
    function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
        $limit = strlen($str);

        for ($s = 0; $start > 0;--$start) {// found the real start
            if ($s >= $limit)
                break;

            if ($str[$s] <= "\x7F")
                ++$s;
            else {
                ++$s; // skip length

                while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
                    ++$s;
            }
        }

        if ($len == '')
            return substr($str, $s);
        else
            for ($e = $s; $len > 0; --$len) {//found the real end
                if ($e >= $limit)
                    break;

                if ($str[$e] <= "\x7F")
                    ++$e;
                else {
                    ++$e;//skip length

                    while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
                        ++$e;
                }
            }

        return substr($str, $s, $e - $s);
    }
}