Php URL Address bar Chinese garbled solution collection

Source: Internet
Author: User
PHP Address bar Chinese $_get down after garbled, UrlEncode and urldecode usage of the detailed
URL encoding
Syntax: string urlencode (String str);
return value: String
Function type: Encoding processing
For example:

<?php$chinesename= "My name is Chinese, oh", $EncodeStr =urlencode ($ChineseName); echo "<a href=/cgi/personal.cgi?name=$ encodestr> my name </a> ";? >

URL decoding
Restores the URL-encoded string.
Syntax: string urldecode (String str);
return value: String
Function type: Encoding processing
For example:
To handle the Chinese in front of the display

<?php$decodestr=urldecode ($_get[' name ');//You may not need to decode, because the browser will automatically decode the echo $DecodeStr for you; >

About PHP using get method from URL to get the problem of Chinese garbled

Use $gonghui = Iconv ("gb2312", "UTF-8", $gonghui), and another method code
/**
* Multi-byte string encoding conversion function
*
* @param string str needs to encode the converted strings
* @param string to_encoding specified to a certain encoding, such as: gb2312, GBK, utf-8, etc.
* @param mixed From_encoding Mix to specify the encoding of the original string, such as: Specify JIS, Eucjp-win, Sjis-win mixed code
* @return String
String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
**/
The Mb_convert_encoding function is an internal multi-byte string encoding conversion function in PHP, which can support almost all encodings where it is needed. PHP >= 4.0.6, version 5 support.

Direct access to reg.php?gh= xxx;

Trade union log-in participation

$gonghui = $_get[' gh '];

Obtained $gonghui for gb2312 encoded output to UTF-8 Web page garbled display

Change into

Trade union log-in Parameters
$gonghui = $_get[' gh '];
$gonghui = mb_convert_encoding ($gonghui, "UTF-8", "gb2312");

It's normal.

Converting an entire page

This method applies to all coding environments. In this way, the character set of the first 128 characters (display character) is used in NCR (Numeric character reference, such as "kanji" will be converted to "Chinese characters" this form), so that the code in any encoding environment, the page can display normally.

Add the following three lines of code to the header of the PHP file:

Mb_internal_encoding ("gb2312");  The gb2312 here is the original code of your website     mb_http_output ("html-entities");     Ob_start (' Mb_output_handler ');

Use the Mb_convert_encoding function to enable the PHP mbstring (multi-byte string) extension.

If there is no mbstring extension for PHP, you need to do the following to allow PHP to support the extension.

1. Windows Server Environment
Edit php.ini file, will; Extension=php_mbstring.dll in front of; Remove and restart the Web server.

2. Linux Server environment
When compiling the configuration, add the--ENABLE-MBSTRING=CN compile parameters, and then compile and install the PHP.

Other users of the third reference method:
Method One UrlDecode
$url = ' aaa.php?region= '. UrlDecode ("Sichuan Province");
<a href= "<?php echo $url;? > ">aaa </a>

Method Two Base64_encode

<?
$test = "Sichuan province";
$test 1=base64_encode ($test);
Echo ' <a href= ' www.jb51.net?region= $test 1 ">aaa </a>";
?>

Another page using Base64_decode to unlock

Base64_decode ($region);

Method Three let the server support Chinese

[Root@dhcp ~]# Locale

Lang=zh_cn.utf-8
Lc_ctype= "Zh_cn.utf-8"
Lc_numeric= "Zh_cn.utf-8"
Lc_time=c
Lc_collate=c
lc_monetary= "Zh_cn.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"
Lc_telephone= "Zh_cn.utf-8"
Lc_measurement= "Zh_cn.utf-8"
lc_identification= "Zh_cn.utf-8"
Lc_all=
[Root@dhcp ~]#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.