How to do URL encoding and decoding in PHP
  John Mwaniki /   21 Dec 2021

How to do URL encoding and decoding in PHP

In this article, we will cover what URL encoding and decoding are, where they are applicable and why, and how to do it using PHP language.

ASCII (American Standard Code for Information Interchange) was the first character encoding standard used between computers and other electronic devices on the Internet.

It was designed in the 1960s, containing 128 characters. These characters include the numbers from 0 to 9, the upper and lower case alphabets from A to Z, and some special characters.

The character sets (encoding) used in modern computers, in HTML, and on the Internet, are all based on ASCII character set. For example, the default character set for HTML 4.01 is ISO-8859-1 while the default in HTML5 is UTF-8, which are both built on ASCII.

The ASCII Reference Table

URLs can only be sent over the Internet using the ASCII character-set. Since oftentimes URLs contain non-ASCII characters (eg. semicolon, equal sign, space, etc), the URL has to be converted into a valid ASCII format.

URL encoding is a mechanism for translating/converting non-ASCII (unprintable or special) characters to a universally accepted format by web servers and browsers, and that can be transmitted over the Internet.

URL encoding replaces the non-ASCII characters with a percent character "%" followed by two hexadecimal digits. These hexadecimal digits represent the numerical value of the characters being replaced.

URLs cannot contain spaces. Therefore, URL encoding replaces spaces with a plus "+" sign or with "%20" depending on the encoding method.

For instance, if you type any URL with some spaces in the browser address bar and hit enter, immediately that URL will be automatically converted to replace the space with "%20".

Example

https://www.example.com/product?=black leather shoe

Will automatically be converted to:

https://www.example.com/product?=black%20leather%20shoe

URL encoding is mostly used in HTML form data submission via HTTP GET requests.

URL encoding is also known as percent-encoding.

As a developer, there will always be scenarios where you will be required to do URL encoding. There are two different ways to do encoding and decoding in PHP which includes:

  1. Using urlencode() and urldecode() functions
  2. Using rawurlencode() and rawurldecode() functions

1. Using urlencode() and urldecode() functions

Also referred to as "application/x-www-form-urlencoded" type, this method is preferable when sending the data submitted from the form to the URL query string.

It uses the PHP built-in functions urlencode() and urldecode() to encode and decode respectively.

This method replaces space with the plus "+" character.

It replaces the special characters with the "%hexcode" except for hyphen (-), underscore (_), and dot (.).

Example: URL encoding

<?php
$url = "https://www.example.com/product?=HP Elitebook Folio 9470m";
$encodedurl = urlencode($url);
echo $encodedurl;

Output:

https%3A%2F%2Fwww.example.com%2Fproduct%3F%3DHP+Elitebook+Folio+9470m

Example: URL decoding

URL decoding simply means reverting an encoded URL back to its original form. To do this, we used the urldecode() function.

<?php
$encodedurl = "https%3A%2F%2Fwww.example.com%2Fproduct%3F%3DHP+Elitebook+Folio+9470m";
$decodedurl = urldecode($encodedurl);
echo $decodedurl;

Output:

https://www.example.com/product?=HP Elitebook Folio 9470m

2. Using rawurlencode() and rawurldecode() functions

This encoding method replaces the spaces within the URL with "%20" as opposed to the above method which uses the plus "+" character.

This encoding method is most preferable when creating URLs dynamically.

This method uses the RFC 3986 standard. Prior to PHP version 5.3.0, it used the RFC 1738 standard.

It uses the PHP built-in functions rawurlencode() and rawurldecode() to encode and decode respectively.

Example: URL encoding

<?php
$url = "https://www.example.com/product?=HP Elitebook Folio 9470m";
$encodedurl = rawurlencode($url);
echo $encodedurl;

Output:

https%3A%2F%2Fwww.example.com%2Fproduct%3F%3DHP%20Elitebook%20Folio%209470m

Example: URL decoding

<?php
$encodedurl = "https%3A%2F%2Fwww.example.com%2Fproduct%3F%3DHP%20Elitebook%20Folio%209470m";
$decodedurl = rawurldecode($encodedurl);
echo $decodedurl;

Output:

https://www.example.com/product?=HP Elitebook Folio 9470m