php如何将汉字转ascii

藏色散人
藏色散人原创
2023-03-05 06:36:011802浏览

php将汉字转ascii的方法:首先创建一个php示例文件;然后通过封装好的“public function strtoascii($str){}”方法将汉字转为ascii即可。

推荐:《PHP视频教程

直接使用吧,函数就在下面,自己封装的,亲测正确无误

将字符串(中文同样实用)转为ascii(注意:我默认当前我们的php文件环境是UTF-8,如果是GBK的话mb_convert_encoding操作就不需要)

public function strtoascii($str){
        $str=mb_convert_encoding($str,'GB2312');
        $change_after='';
        for($i=0;$i<strlen($str);$i++){
            $temp_str=dechex(ord($str[$i]));
            $change_after.=$temp_str[1].$temp_str[0];
        }
        return strtoupper($change_after);
    }

以上就是php如何将汉字转ascii的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。