How does PHP encode and decode Chinese characters in Url? (Pictures + Videos)

藏色散人
Release: 2018-09-28 16:26:17
Original
6856 people have browsed it

This article mainly introduces to you the specific implementation method of PHP encoding and decoding Chinese characters in the URL.

Some novice friends may be a little unfamiliar with the concept of url encoding and decoding. But if we put it this way, when we browse major web pages, we may find that some URLs have some special symbols such as #, &, _ or Chinese characters, etc. In order to comply with the URL specifications, there are URLs with these symbols. It needs to be encoded. With this simple explanation, do you know something about url encoding and decoding?

url encoding and decoding, also called percent encoding, is the Uniform Resource Locator (URL) encoding method.

Below we combine simple code examples to introduce PHP's method of encoding and decoding Chinese characters in url.

1. url encoding (urlencode)

<?php
$url = urlencode(&#39;PHP中文网&#39;);//把 PHP中文网 进行编码
$password = md5(123123);
echo "index.php?user=$url&password=$password" ;
Copy after login

In this code, we use the urlencode function to encode the "PHP Chinese website", and Use md5 encryption, and finally output the spliced ​​URL for testing.

The result is as shown below:

How does PHP encode and decode Chinese characters in Url? (Pictures + Videos)

As shown in the figure, the three Chinese characters "中文网" were successfully encoded, all with percent signs, numbers and letters. Displayed in combined form. And the original password "123123" has also been encrypted.

Note: urlencode represents the encoded URL string

The urlencode return value represents the returned string, except -_. All non-alphanumeric characters except are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as a plus sign ( ).

2. url decoding (urldecode)

<?php
$url = urlencode(&#39;PHP中文网&#39;);//把 PHP中文网 进行编码
$password = md5(123123);
echo "index.php?user=$url&password=$password" . &#39;<br>&#39;;
$url = urldecode("%E4%B8%AD%E6%96%87%E7%BD%91");  // 把编码还原成 PHP中文网
echo $url;
Copy after login

Then we decode the encoded Chinese characters. Here we mainly use the urldecode function in PHP.

The test results are as follows:

How does PHP encode and decode Chinese characters in Url? (Pictures + Videos)

As shown in the figure, the garbled characters behind PHP have been decoded into Chinese.

Note: urldecode means decoding the encoded URL string

The return value of urldecode means returning the decoded string.

For url encoding and decoding methods, you mainly need to master the two functions urlencode and urldecode.

URL encoding is mainly to comply with URL specifications or prevent SQL injection.

This article is an introduction to the relevant knowledge about url encoding and decoding in PHP. I hope it will be helpful to friends in need.

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How does PHP encode and decode Chinese characters in Url? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!