Php URL Address bar pass Chinese garbled solution collection _php skills

Source: Internet
Author: User
Tags character set urlencode

PHP Address bar to pass Chinese $_get down after garbled, UrlEncode and UrlDecode usage detailed
URL encoding
Syntax: string urlencode (String str);
return value: String
Function type: Encoding processing
For example:

Copy Code code as follows:

<?php
$ChineseName = "My name is Chinese."
$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 the Chinese in front of the processing display
Copy Code code as follows:

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

about the Chinese garbled problem that PHP obtains from the URL with the Get method

Use $gonghui = Iconv ("gb2312", "UTF-8", $gonghui); Another method code
/**
* Multi-byte string encoding conversion function
*
* @param string STR requires a coded conversion
* @param string to_encoding Specifies conversion to some encoding, such as: gb2312, GBK, utf-8, etc.
* @param mixed from_encoding to mix and specify the encoding of the original string, such as: JIS, Eucjp-win, Sjis-win mixed encoding
* @return String
String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
**/
The mb_convert_encoding function is a multiple-byte string encoding conversion function within PHP that can support almost all encodings where needed. PHP >= 4.0.6, version 5 support.

Direct access to reg.php?gh= so-and-so;

Union Login

$gonghui = $_get[' gh '];

Obtained $gonghui for gb2312 encoded output to Utf-8 page on the display garbled

Change into

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

It's normal.

To convert an entire page

This method is applicable to all coding environments. In this way, the character set of the first 128 characters (displayed characters) is represented by NCR (Numeric character reference, such as "Kanji", which is converted to "kanji"), so that the encoding can be displayed correctly in any coded environment.

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

Copy Code code as follows:

Mb_internal_encoding ("gb2312"); The gb2312 here is the original code for your site.
Mb_http_output ("Html-entities");
Ob_start (' Mb_output_handler ');

You use the Mb_convert_encoding function to enable the mbstring (multi-byte string) extension of PHP.

If you don't have mbstring extensions for PHP, you'll need to make the following settings so that PHP supports 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 compilation parameters, and then compile the PHP installation.

Other user's third reference method:
Method A 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 uses Base64_decode to unlock

Base64_decode ($region);

Method Three enables the server to 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.