首頁  >  文章  >  後端開發  >  php url網址列傳中文亂碼解決方法集合

php url網址列傳中文亂碼解決方法集合

高洛峰
高洛峰原創
2017-01-12 15:02:241602瀏覽

php位址列傳中文$_GET下來後亂碼,urlencode和urldecode用法詳解
url編碼
語法: string urlencode(string str);
回傳值: 字串
函數種類: 編碼處理

還原URL 編碼字串。

語法: string urldecode(string str);
回傳值: 字串
函數種類: 編碼處理
例如:
對前面傳過來的中文進行處理顯示

<?php
$ChineseName="我的名字,是中文的哦";
$EncodeStr=urlencode($ChineseName);
echo "<a href=/cgi/personal.cgi?name=$EncodeStr>我的名字</a>";
?>

關於php用get方法從url上獲得的中文亂碼問題 

使用$gonghui = iconv("gb2312","UTF-8",$gonghui);另一方法程式碼 
/**
* 多位元組字串編碼轉換函數
*
* @param string str 需要編碼轉換的字串
* @param string to_encoding 指定轉換為某種編碼,如:gb2312、gbk、utf-8等
* @?*/
mb_convert_encoding 函數為php內部多位元組字串編碼轉換函數,可以在有需要的使用場合,幾乎支援所有編碼。 PHP  >= 4.0.6、 5 版本支援。

直接取得reg.php?gh=某某;

 //工會登錄參

$gonghui = $_GET['gh'];

取得的$gonghui 為gb2312編碼網頁輸出到亂碼

改成

 //工會登入參數

 $gonghui = $_GET['gh'];

 $gonghui = mb_convert_encoding($gonghui, "UTF-8", "gb2312");

對整個頁面進行轉換


該方法適用所有編碼環境。這樣把前128個字符以外(顯示字符)的字符集都用   NCR(Numeric character reference,如“漢字”將轉換成“漢字”這種形式)來表示,這樣的編碼在任意編碼環境下頁面都能正常顯示。

在php檔案的頭部加上下面三行程式碼:

<?php
$DecodeStr=urldecode($_GET[&#39;name&#39;]);//你可能不用解码都可以,因为浏览器会自动帮你解码
echo $DecodeStr;
?>

使用mb_convert_encoding 函數需啟用PHP 的mbstring (multi-byte string)擴充。

如果沒有沒有開啟php的mbstring擴展,則需要做以下設置,讓php支援該擴展。

1、windows 伺服器環境

編輯 php.ini 文件,將; extension=php_mbstring.dll 前面的 ; 去掉,重啟網頁伺服器。

2、Linux伺服器環境

在編譯設定時加入 --enable-mbstring=cn 編譯參數,再進行PHP的編譯安裝。

其它網友的第三個參考方法:

//方法一urldecode
其它網友的第三個參考方法:

//方法一urldecode

$url = 'aaa.php?region='.urldecode("四川省");
aaa

//方法二base64_encode

 
$test="四川省";

$test1=base64_encode($test);

echo 'aaa ';

?>


另一頁使用base64_decode解開

base64_decode($region);

//方法三讓伺服器支援中文

[ root@dhcp ~]# locale

lang=zh_cn.utf-8

lc_ctype="zh_cn.utf-8"

lc_numeric="zh_cn.utf-8"

lc_time=ccng_collat​​_collat​​_collat​​​​lc. utf-8"

lc_messages="zh_cn.utf-8"

lc_paper="zh_cn.utf-8"

lc_name="zh_cn.utf-8"
lc_address="zh_cn.utf-8"
_teleph
lc_address="zh_cn.utf-8"
_telephone=" utf-8"
lc_measurement="zh_cn.utf-8"
lc_identification="zh_cn.utf-8"
lc_all=
[root@dhcp ~]#

更多亂碼解決地址列傳遞相關中文方法集相關中文文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn