php 转换编码为utf-8

2022-09-11 12:29:25 浏览数 (1)

代码语言:javascript复制
/**
     * 编码改成utf-8
     * @param $str
     * @return bool|null|string|string[]
     */
    public static function strToUtf8($str)
    {
        $encode = mb_detect_encoding($str, ["ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5']);
        if ($encode == 'UTF-8') {
            return $str;
        } else {
            return mb_convert_encoding($str, 'UTF-8', $encode);
        }
    }

0 人点赞