PHP supports URL encoding decoding options with the use of its core functions. PHP urlencode() and urldecode() functions are the built-in functions which usually help to perform URL encoding and decoding respectively.
In this tutorial, we are going to see the type of URL encoding type, and PHP functions to encode/decode based on the standard with examples.
Encoding is an important job in various scenarios. URL encoding helps to convert the URL by adding standard entities into it. For example, if an URL contains non-alphanumeric characters then the encoding will replace those characters with the encoding entities.
Some special characters are exceptional and will not be replaced. This will help to prevent data truncation, and jumbling while accessing the URL data.
The encoding is required to be performed before sending URL data to a query string or to a function that might dynamically work on this URL data.
And then, the encoded data will be decoded into its original form, after receiving it in target or landing the PHP code block. Decoding is the process of reversing the encoded data back to form.
In PHP, URL encoding and decoding can be done in two different ways. Those types are listed below. Based on the content type to be encoded, choosing the encoding method will be meaningful and effective.
In the following sections, we will see how to apply PHP encoding decoding based on the above types of standards.
For performing this type of encoding or decoding, the PHP built-in functions urlencode() and urldecode() are used. We can prefer this type when we need to send the data submitted from the form to the URL query string.
This function will replace the special characters except (_), (-) and (.) that occur in the given URL with %[hex code]; space will be replaced with ‘+’ character.
If we send a URL without encoding, then some special characters may truncate the actual data passed through the page URL. Then It will cause errors or unexpected results.
The following example code will show how to apply encoding decoding on a URL by using PHP urlencode() urldecode() functions. In this script, the URL is assigned to a PHP variable. Then this variable will be passed to the urlencode() function.
This function will return the encoded URL. To revert this encoded URL back to its original form, the urldecode() will be used.
<?php $url = "https://phppot.com/home.php?name=all about php"; $encodedUrl = urlencode($url); // returns http%3A%2F%2Fphppot.com%2Fhome.php%3Fname%3Dall+about+php echo $encodedUrl; // returns https://phppot.com/home.php?name=all about php echo urldecode($encodedUrl); ?>
PHP functions rawurlencode() and rawurldecode() are used to encode and decode the URL with this type. By using this method the space in the URL could be replaced with %[hex code] instead of the plus (+) symbol. This type of encoding will be preferable when we need to create URL dynamically.
As of PHP version 5.3.0 and later, the rawurlencode() uses the RFC 3986 standard. Prior to this version, it followed RFC 1738 standard.
This code shows how to apply the rawurlencode(). There is no difference in using these encoding decoding functions.
It will be very similar to the usage of the urlencode() urldecode() functions. The expected output is stated by using the PHP comment statement. Run this program in your PHP environment and check if you get the same output as it is in the below code.
<?php $url = "https://phppot.com/home.php?name=all about php"; $encodedUrl = rawurlencode($url); // returns http%3A%2F%2Fphppot.com%2Fhome.php%3Fname%3Dall%20about%20php echo $encodedUrl; // returns https://phppot.com/home.php?name=all about php echo rawurldecode($encodedUrl); ?>