The urldecode() function in PHP is used to decode URLs

WBOY
Release: 2023-11-18 10:10:01
Original
1080 people have browsed it

The urldecode() function in PHP is used to decode URLs

The urldecode() function in PHP is used to decode the URL and restore the escaped characters to the original characters. When processing parameters in the URL, you often need to use the urldecode() function.

The syntax of the urldecode() function is as follows:
string urldecode (string $str)

Among them, $str is the URL string to be decoded. The urldecode() function will replace %xx in the string with the corresponding character, where xx represents the hexadecimal representation of the ASCII code value of the character.

The following is a specific code example that shows how to use the urldecode() function for URL decoding:

<?php
$url = "https%3A%2F%2Fwww.example.com%2F%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D20";
$decodedUrl = urldecode($url);

echo "原始URL:" . $url . "<br>";
echo "解码后的URL:" . $decodedUrl . "<br>";

parse_str(parse_url($decodedUrl, PHP_URL_QUERY), $params);
echo "URL参数:<br>";
foreach ($params as $key => $value) {
    echo $key . ":" . $value . "<br>";
}
?>
Copy after login

$url in the above code is the URL character encoded by the urlencode() function String, representing a URL containing parameters. Use the urldecode() function to decode the URL and assign the decoded URL string to $decodedUrl.

Next, call the parse_str() function to parse the decoded URL string into an associative array, which contains the parameters in the URL and their corresponding values. Finally, by traversing the associative array, we can obtain the specific parameters and their values ​​in the URL.

Execute the above code and you will get the following output:

Original URL: https://www.example.com/?name=张三&age=20
Decoded URL : https://www.example.com/?name=张三&age=20
URL parameters:
name: 张三
age: 20

You can see that after decoding The resulting URL string is restored to the original URL, and the parameters in the URL are correctly parsed and printed.

The urldecode() function is very useful when processing parameters in the URL, and can ensure the correct transmission and processing of parameters. When developing web applications, especially when processing URL parameters entered by users, we often use the urldecode() function to ensure the correctness and validity of the data.

The above is the detailed content of The urldecode() function in PHP is used to decode URLs. 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!