URL Encoding and Decoding with PHP

PHP provides us with the ability to encode and decode URLs with the implementation of two main functions. However, the question is, why is it used? Encoding and decoding URL strings are used to convert general URL strings and characters into an arrangement that can be conveyed over the internet. In this tutorial, you will learn about two ways in which URL string can be encoded and decoded in PHP.

URL Encoding with PHP

Encoding plays an important role in different scenarios of technology and programming. URL encoding is used in PHP to convert the URL by including typical entities or characters into it. Usually, when an URL holds non-alphanumeric characters, these characters will be encoded, which will replace these characters with some specific encoding entities. However, some distinct exceptional characters cannot be replaced.

Such an encoding mechanism is essential to be achieved before sending the URL data to query string or in a function for making it work dynamically on any URL data. Once the work is done on that encoded URL, these encoded data is then the URL is decoded into its original form.

urlencode() function in PHP is used for encoding a string associated with the URL. This function is responsible for encoding in the same manner data posted on the web page is encoded. This function will return an encoded string when executed.

The syntax of using this function is:

urlencode( string $str )

Characteristics of URL Encoding

  • While the internet sends the URL, it is only possible if its characters are in the ASCII format.
  • PHP's encoding schemes will replace the unsafe ASCII characters with a "%" tailed by 2 hex-digit.
  • The URL must be converted to an effective ASCII character-set because URLs often contain characters outside the ASCII set.
  • An URL cannot have any spaces, so these spaces are replaced with either the '%20' or '+' (plus) symbol.

The urlencode() Function Example

An example of using this URL encoding function is as follows:


//PHP Program of urlencode() Function

echo urlencode("https://w3schools.in/");

Program Output:

URL Decoding with PHP

Another function called the urldecode() function is another inbuilt function of PHP and is implemented for decoding the URL, encoded by the urlencode() function. Decoding is the approach of reversing the non-ASCII data back to its original form. This function will accept a single string as its parameter. That string will contain the encoded URL to be decoded.

The urldecode() Function Example

An example of using this URL decoding function is as follows:


//PHP Program of urldecode() Function

echo urldecode("https%3A%2F%2Fw3schools.in%2F");

Program Output:

Types of URL Encoding and Decoding in PHP

Encoding and decoding can be done in two different approaches in PHP. These are:

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

RFC 3986 standard type

This is a type of encoding-decoding approach where the PHP functions rawurlencode() and rawurldecode() are implemented to encode and decode the URL, respectively. This is not a part of this tutorial. Here, URL spaces are replaced with %[hex code] rather than the plus (+) symbol.

application/x-www-form-urlencoded type

This is a type of encoding-decoding approach where the built-in PHP functions urlencode() and urldecode()are implemented to encode and decode the URL, respectively. This encoding will replace almost all the special characters other than (_), (-), and (.) in the given URL. Space is also replaced with a (+) plus sign rather than a %[hex code].

Scroll Back to Top