PHP利用get擷取url漢字出現亂碼問題的解決方案

來源:互聯網
上載者:User
這篇文章主要介紹了PHP採用get擷取url漢字出現亂碼的解決方案,是很多PHP程式員都曾遇到的問題,非常具有實用價值,需要的朋友可以參考下

本文執行個體講述了PHP採用get擷取url漢字出現亂碼的解決方案。分享給大家供大家參考。具體方法如下:

一、問題:

本來打算這樣使用

<a href="list.php?plate=轄區動態" charset="utf-8" target="main">[查看轄區動態]</a>

結果在list.php頁面得到的是—–查看[轄區動懌]
一開始猜想可能是漢字的“態"這個編碼和某些東西衝突了,所以出現亂碼。

二、解決方案:

使用:

<a href="list.php?plate=<?php echo urlencode("轄區動態");?>" charset="utf-8" target="main">[查看]</a>

然後在list.php頁面這樣用

<?phpheader("Content-type: text/html; charset=utf-8");if($_GET['plate'])echo $plate=urldecode($_GET['plate']);?>

也就不會亂碼和傳遞不正常了。
注意此處的接收GET頁面編碼需要和發送端一致!

關於string urlencode ( string $str )函數

此函數便於將字串編碼並將其用於 URL 的請求部分,同時它還便於將變數傳遞給下一頁。
樣本1 urlencode()

<?phpecho '<a href="mycgi?foo=', urlencode($userinput), '">';?>

樣本2 urlencode() 與 htmlentities()

<?php$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);echo '<a href="mycgi?' . htmlentities($query_string) . '">';?>

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.