php utf-8字符转ascii字符

时间:2023-01-05 18:52:03

function utf8_urldecode($str) 

{
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
return html_entity_decode($str,null,'UTF-8');;
}

function ConvertUTF8ToGB2312($str)
{
return iconv("UTF-8","GB2312//IGNORE",$str);
}

function ConvertGB2312ToUTF8($str)
{
return iconv("GB2312","UTF-8//IGNORE",$str);
}

php处理环境是utf-8的, jquery所在网页也是的utf-8的, jquery post的中文数据以%u开头, 需要将这个code转发到另一台接收gb2312字符的web网站

if(isset($_REQUEST["code"]))
{
    //解码
    $tmp = utf8_urldecode($_REQUEST["code"]);
    //utf-8转gb2312
    $tmp = ConvertUTF8ToGB2312($tmp);
    //编码
    $tmp = urlencode($tmp);
    $AppendPara .= "&code=" . $tmp;
}
else
{
    $AppendPara .= "&code=";
}