Home  >  Article  >  Backend Development  >  What should I do if the Chinese characters in the URL obtained by PHP are garbled?

What should I do if the Chinese characters in the URL obtained by PHP are garbled?

coldplay.xixi
coldplay.xixiOriginal
2020-08-17 09:28:011626browse

PHP uses get to get the solution to garbled Chinese characters in the URL: use the function [string urlencode (string $str)] to encode the string and use it in the request part of the URL. The code is [echo '< ;a href="mycgi?foo=', url].

What should I do if the Chinese characters in the URL obtained by PHP are garbled?

##Solution to the garbled Chinese characters in the url obtained by PHP using get:

1. Use:

The code is as follows:

<a href="list.php?plate=<?php echo urlencode("辖区动态");?>" charset="utf-8" target="main">[查看]</a>

Then use

on the list.php page and the code is as follows:

<?php
header("Content-type: text/html; charset=utf-8");
if($_GET[&#39;plate&#39;])
echo $plate=urldecode($_GET[&#39;plate&#39;]);
?>

There will be no garbled codes and abnormal transmission.

Note that the encoding of the receiving GET page here needs to be consistent with the sending end!

2. About

string urlencode ( string $str ) Function

This function facilitates encoding a string and using it in the request part of the URL, and it also facilitates passing variables to the next page.

Example 1

urlencode()

The code is as follows:

<?php
echo &#39;<a href="mycgi?foo=&#39;, urlencode($userinput), &#39;">&#39;;
?>

Example 2

urlencode() andhtmlentities()

Code As follows:

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

Related learning recommendations:

php programming (video)

The above is the detailed content of What should I do if the Chinese characters in the URL obtained by PHP are garbled?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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