PHP语言汉字转为ASCII码

风之舞 2024-02-01 17:09:32 102
简介: PHP语言汉字转为ASCII码
function ascii_encode($str)
{
    $str = mb_convert_encoding($str, 'UTF-8');
    $asc = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $temp_str = dechex(ord($str[$i]));
        $asc .= $temp_str[0].$temp_str[1];
    }
    return strtoupper($asc);
}
function ascii_decode($sacii)
{
    $asc = str_split(strtolower($sacii), 2);
    $str ='';
    for ($i = 0; $i < count($asc); $i++) $str.= chr(hexdec($asc[$i][0].$asc[$i][1]));
    return mb_convert_encoding($str, 'UTF-8');
}