Home > Backend Development > PHP Tutorial > How to use urlencode() function for url encoding in PHP? (code example)

How to use urlencode() function for url encoding in PHP? (code example)

青灯夜游
Release: 2023-04-05 20:28:02
Original
14018 people have browsed it

The urlencode() function is a built-in function in PHP used to encode urls. The following article will introduce to you some usage of the urlencode() function, so that you can understand how the urlencode() function encodes URLs. I hope it will be helpful to you.

[Video tutorial recommendation: PHP tutorial]

How to use urlencode() function for url encoding in PHP? (code example)

## The urlencode() function accepts a url as a parameter and returns a string. All non-alphanumeric characters contained in this string, except -_., are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs ( ).

Recommended manual:php complete self-study manual

Basic syntax

string urldecode( $input )
Copy after login

Parameters :This function accepts a single parameter $input, which holds the url to be encoded.

Return value: The urlencode() function successfully returns the encoded string.

Let’s take a look at how the PHP urlencode() function encodes URLs through code examples.

Example 1:

<?php 
echo urlencode("PHP:s? hg+3\c=67 "); 
?>
Copy after login

Output:

PHP%3As%3F+hg%2B3%5Cc%3D67+
Copy after login

Example 2:

<?php 
echo urlencode("//m.sbmmt.com/") . "<br>"; 
echo urlencode("http://www.baidu.com/?tn=62095104_26_oem_dg") . "<br>"; 
?>
Copy after login

Output:

http%3A%2F%2Fm.sbmmt.com%2F
http%3A%2F%2Fwww.baidu.com%2F%3Ftn%3D62095104_26_oem_dg
Copy after login

Recommended related articles: 1.
How does PHP encode and decode Chinese characters in Url? (Pictures and videos)
Related video recommendations: 1.
Dugu Jiujian (4)_PHP video tutorial
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use urlencode() function for url encoding in PHP? (code example). 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