PHP URL Encoding Decoding

by Vincy. Last modified on July 2nd, 2022.

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.

PHP URL Encoding Decoding

URL Encoding Decoding Types in PHP

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.

  1. application/x-www-form-urlencoded type
  2. RFC 3986 standard type

In the following sections, we will see how to apply PHP encoding decoding based on the above types of standards.

application/x-www-form-urlencoded type

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.

PHP Encoding Decoding Example with urlencode() urldecode()

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.

$url = " about php";
$encodedUrl = urlencode($url);
// returns
echo $encodedUrl;
// returns about php
echo urldecode($encodedUrl);

RFC 3986 standard type

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.

PHP Example with  rawurlencode() and rawurldecode()

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.

$url = " about php";
$encodedUrl = rawurlencode($url);
// returns
echo $encodedUrl;
// returns about php
echo rawurldecode($encodedUrl);
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP URL Encoding Decoding”

  • Gideon Birimuye says:

    Hi Vincy, Ive been farming on your blog and i have found it interesting (php wise). A week back i started programming in php and I’m having fun with it. Now that i have found you i believe you can be a handy tutor as regards helping me understand this language. I’m looking forward to sharing more with you.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page