Phpurl address bar-a set of solutions for garbled Chinese characters

Source: Internet
Author: User
Php address bar uploads Chinese $ _ GET and garbled characters, urlencode, urldecode, iconv, base64_encode and other methods, sorting is basically common. Php address bar uploads Chinese $ _ GET and garbled characters. usage of urlencode and urldecode
Url encoding
Syntax: string urlencode (string str );
Return value: string
Function type: encoding
For example:
The code is as follows:
$ ChineseName = "My name is Chinese ";
$ EncodeStr = urlencode ($ ChineseName );
Echo "My Name ";
?>

Url decoding
Returns the URL encoded string.
Syntax: string urldecode (string str );
Return value: string
Function type: encoding
For example:
Process and display the previously passed Chinese characters
The code is as follows:
$ DecodeStr = urldecode ($ _ GET ['name']); // You may not need to decode it, because the browser will automatically help you decode it.
Echo $ DecodeStr;
?>

Chinese garbled characters obtained from URLs using the get method in php

Use $ gonghui = iconv ("gb2312", "UTF-8", $ gonghui); another method of code
/**
* Multi-byte string encoding conversion function
*
* @ Param string str the string to be encoded and converted
* @ Param string to_encoding specifies the conversion to a certain encoding, such as gb2312, gbk, and UTF-8.
* @ Param mixed from_encoding: specify both the encoding of the original string, for example, JIS, eucjp-win, and sjis-win.
* @ Return string
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding])
**/
The mb_convert_encoding function is a multi-byte character string conversion function in php. it can be used as needed and supports almost all encoding. PHP> = 4.0.6 and 5.

Get reg. php directly? Gh = xx;

// Trade union login parameters

$ Gonghui = $ _ GET ['GH'];

The obtained $ gonghui is gb2312 encoded and output to the UTF-8 webpage with garbled characters.

Change

// Union login parameters
$ Gonghui = $ _ GET ['GH'];
$ Gonghui = mb_convert_encoding ($ gonghui, "UTF-8", "gb2312 ");

It is displayed as normal.

Convert the entire page

This method applies to all encoding environments. In this way, the character sets other than the first 128 characters (display characters) are expressed by NCR (Numeric character reference, for example, "Chinese characters" will be converted to "Chinese characters, such encoding can be normally displayed on pages in any encoding environment.

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

The code is as follows:
Mb_internal_encoding ("gb2312"); // Here, gb2312 is the original code of your website.
Mb_http_output ("HTML-ENTITIES ");
Ob_start ('MB _ output_handler ');

To use the mb_convert_encoding function, you must enable the mbstring (multi-byte string) extension of PHP.

If the php mbstring extension is not enabled, you need to make the following settings to allow php to support the extension.

1. windows Server environment
Edit the php. ini file, and replace the file before extension = php_mbstring.dll. remove the file and restart the web server.

2. Linux server environment
Add the -- enable-mbstring = cn compilation parameter during compilation configuration, and then compile and install PHP.

Third Reference Method for other netizens:
// Method 1 urldecode
$ Url = 'AAA. php? Region = '. urldecode ("Sichuan province ");
"> Aaa

// Method 2: base64_encode

$ Test = "Sichuan province ";
$ Test1 = base64_encode ($ test );
Echo 'AAA ';
?>

Use base64_decode to unlock the other page

Base64_decode ($ region );

// Method 3 enable the server to support Chinese characters

[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.