php中文转换ascii码的方法:首先设置当前php文件环境为UTF-8;然后通过“public function strtoascii($str){...}”将中文转换为ascii码即可。
php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用
本文操作环境:Windows7系统、PHP7.1版,DELL G3电脑
php 中文怎么转换ascii码?
php将字符串转为ASCII,php将中文汉字字符串转为ASCII,以及互转
直接使用吧,函数就在下面,自己封装的,亲测正确无误
A:将字符串(中文同样实用)转为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);}
登录后复制
B:将ascii转为字符串(中文同样实用)(注意:我默认当前我们的php文件环境是UTF-8,如果是GBK的话mb_convert_encoding操作就不需要)
public function asciitostr($sacii){$asc_arr= str_split(strtolower($sacii),2);$str='';for($i=0;$i<count($asc_arr);$i++){$str.=chr(hexdec($asc_arr[$i][1].$asc_arr[$i][0]));}return mb_convert_encoding($str,'UTF-8','GB2312');}
登录后复制
推荐学习:《PHP视频教程》
以上就是php 中文怎么转换ascii码的详细内容,更多请关注php中文网其它相关文章!