找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

php如何实现将字符串转为ASCII

作者:模板之家 2021-10-15 20:23 113人关注

php实现将字符串转为ASCII的方法:【public function strtoascii($str{ $str=mb_convert_encoding($str,'GB2312');$change_after='';for(...】。

php实现将字符串转为ASCII的方法:【public function strtoascii($str{ $str=mb_convert_encoding($str,'GB2312');$change_after='';for(...】。

本文操作环境:windows10系统、php 7、thinkpad t480电脑。

字符串与ASCII之间的转换:

下面的函数是已经封装好的,可以直接拿来使用。

1、将字符串(中文同样实用)转为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);
    }

2、将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的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)