Updated March 29, 2023
Introduction to PHP urlencode
Dealing with URLs and parameterized URLs are common for any programming language. The urlencode() is a function that can be used to encode the URL. When we pass any value with the URLs, it gets modified automatically through the program. Let’s understand it through an example URL; the actual URL is “https://www.educba.com/cart/?pid=5217&ptype=11&color=green and red”, after encoding this URL it will be“https%3A%2F%2Fwww.educba.com%2Fcart%2F%3Fpid%3D5217%26ptype%3D11%26color%3Dgreen+and+red”. The difference between the actual URL and the encoded URL can be seen easily. In some cases, both the actual URLs and the encoded URLs could be the same; it totally depends upon the parameters defined inside that URL. After using the urldecode(), we can get the actual URL back. In this topic, we are going to learn about PHP urlencode.
Syntax
urlencode($strURL);
Explanation:
$strURL – $strURL is the URL itself. This is the mandatory parameter. The type of this parameter is a string. The return type of this function is also a string. Using this urlencode() will not be sufficient for any developer or the user, along with we also have to go for the urldecode() so that we can reach back to the actual or the original URL. So, here is the syntax for the URL decode.
urldecode($strURL);
As we can even the decode URL function have the same parameter. But in the parameter, it will be an encoded string or URL. So if we will pass the actual string, not the decoded one, then the output could be different than the actual string.
How does urlencode work in PHP?
The URL encoding comes into existence when we need to pass various types of parameters with some value from one place to another. This is one of the required things so that we can get the actual value accurately at the destination page. We can only get the URL encoded value back to the actual value after applying the urldecode() function. The syntax and the semantics of the urldecode() will remain the same as we can see in the urlendode(). There are various in-built functions to do this job in the PHP language, but urlencode() is one of the most widely used.
Here are the two built-in functions that can be used together to get the job done:
urlencode() – for encoding string
urldecode() – for decoding back to the actual string
Examples of using urlencode
Different examples are mentioned below:
Example #1
A simple of using the urlencode() on various URLs.
Code:
<?php
// Here is the list of example of URL encode
echo "https://www.educba.com/blog/?source=menu" . " => ". urlencode("https://www.educba.com/blog/?source=menu") . "<br>";
echo "https://www.educba.com/tutorials/?source=menu" . " => ". urlencode("https://www.educba.com/tutorials/?source=menu") . "<br>";
echo "https://www.educba.com/courses/?source=menu" . " => ". urlencode("https://www.educba.com/courses/?source=menu") . "<br>";
echo "https://cdn.educba.com/academy/wp-content/uploads/2020/05/cropped-website_logo_transparent_background_white.png" . " => ". urlencode("https://cdn.educba.com/academy/wp-content/uploads/2020/05/cropped-website_logo_transparent_background_white.png") . "<br>";
?>
Output:
Example #2
Let’s try to decode the same URLs as mentioned above to get the same in the actual form. In PHP the for every encoding, there is decoding in general.
Code:
<?php
// Here is the list of example of URL encode
echo "https://www.educba.com/blog/?source=menu" . " => ". urlencode("https://www.educba.com/blog/?source=menu") . "<br>";
echo "https://www.educba.com/tutorials/?source=menu" . " => ". urlencode("https://www.educba.com/tutorials/?source=menu") . "<br>";
echo "https://www.educba.com/courses/?source=menu" . " => ". urlencode("https://www.educba.com/courses/?source=menu") . "<br>";
echo "https://cdn.educba.com/academy/wp-content/uploads/2020/05/cropped-website_logo_transparent_background_white.png" . " => ". urlencode("https://cdn.educba.com/academy/wp-content/uploads/2020/05/cropped-website_logo_transparent_background_white.png") . "<br>";
echo "<br> <br><b>Putting coding back to the actul form:</b>";
echo "<pre>";
echo urldecode(urlencode("https://www.educba.com/blog/?source=menu")) . "<br>";
echo urldecode(urlencode("https://www.educba.com/tutorials/?source=menu")) . "<br>";
echo urldecode(urlencode("https://www.educba.com/courses/?source=menu")) . "<br>";
echo urldecode(urlencode("https://cdn.educba.com/academy/wp-content/uploads/2020/05/cropped-website_logo_transparent_background_white.png")) . "<br>";
?>
Output:
Encoding is all about putting something into the coded form that could be more difficult to read or understand comparatively. It is not to just play with encoding and decoding; this is one of the basic needs in any programming language. When we send the + sign in the URL, it gets changes to some other value, and we cannot access this as it is in all cases.
Example #3
The encode is all about replacing some characters with other ones. In general, the urlencode() returns a string in which we can see that all the non-alphanumeric characters except -, . etc. have been replaced. According to the characters, these replacements will be done with a percent (%) sign followed by some hex digits. So let’s have a close look upon the same using an example.
Code:
<?php
$string = 'This is a very hot day today in this rainy season - 2020.';
$pathModified = urlencode($string);
?>
<?php echo $pathModified; ?>
Output:
In the above example, we can see the below changes:
Space – space has been replaced with the + sign.
Dot (-) – As per the rule, there is no change or replacement.
Example #4
Let’s see another example of urencode() with some special character in the string.
Code:
<?php
$string = 'Hellö _ Wörld';
$pathModified = urlencode($string);
echo $pathModified;
?>
Output:
In the output of the above program, we can see that special characters have been replaced.
Example #5
Let’s see an example of urdecode() with some special character in the string.
Code:
<?php
$Destring = 'Hell%C3%B6+W%C3%B6rld';
$pathModified = urldecode($Destring);
echo "Decoded String: ".$Destring . "<br>Actual String: ". $pathModified;
?>
Output:
In the output of the above program, we can see that the encoded string is getting into the actual string (as in the previous program).
Conclusion
The function urlencode() can be used to decode a string or any parameterized URLs. PHP also has a built-in function the decode back the original string named urldecode(). The urlencode() and the urldecode() can be used in pairs to get the job done; Both of these PHP functions are complementary to each other. PHP also has various other coding and decoding string functions we can move ahead with, but these two are one of the most popular and most widely used functions.
Recommended Articles
This is a guide to PHP urlencode. Here we discuss the introduction, syntax, and working of urlencode in PHP along with different examples and code implementation. You may also have a look at the following articles to learn more –