- How to do URL encoding and decoding in PHP
- Example
- 1. Using urlencode() and urldecode() functions
- Example: URL encoding
- Example: URL decoding
- 2. Using rawurlencode() and rawurldecode() functions
- Example: URL encoding
- Example: URL decoding
- Related Articles
- urldecode
- Parameters
- Return Values
- Examples
- Notes
- See Also
- User Contributed Notes 20 notes
- Using PHP urlencode and urldecode
- Why Encode and Decode Strings to URLs
- Encoding Strings With urlencode() and rawurlencode()
- URL Encoding and Decoding with PHP
- URL Encoding with PHP
- Characteristics of URL Encoding
- The urlencode() Function Example
- URL Decoding with PHP
- The urldecode() Function Example
- Types of URL Encoding and Decoding in PHP
- RFC 3986 standard type
- application/x-www-form-urlencoded type
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.
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:
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:
- Using urlencode() and urldecode() functions
- 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
Example: URL decoding
URL decoding simply means reverting an encoded URL back to its original form. To do this, we used the urldecode() function.
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
Example: URL decoding
https://www.example.com/product?=HP Elitebook Folio 9470m
Related Articles
urldecode
Decodes any % ## encoding in the given string. Plus symbols (‘ + ‘) are decoded to a space character.
Parameters
Return Values
Returns the decoded string.
Examples
Example #1 urldecode() example
if ( $param ) printf ( «Value for parameter \»%s\» is \»%s\»
\n» , urldecode ( $param [ 0 ]), urldecode ( $param [ 1 ]));
>
>
?>
Notes
The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.
See Also
- urlencode() — URL-encodes string
- rawurlencode() — URL-encode according to RFC 3986
- rawurldecode() — Decode URL-encoded strings
- » RFC 3986
User Contributed Notes 20 notes
When the client send Get data, utf-8 character encoding have a tiny problem with the urlencode.
Consider the «º» character.
Some clients can send (as example)
foo.php?myvar=%BA
and another clients send
foo.php?myvar=%C2%BA (The «right» url encoding)
in this scenary, you assign the value into variable $x
$x = $_GET [ ‘myvar’ ];
?>
$x store: in the first case «�» (bad) and in the second case «º» (good)
To fix that, you can use this function:
function to_utf8 ( $string ) <
// From http://w3.org/International/questions/qa-forms-utf-8.html
if ( preg_match ( ‘%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF] # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF] # planes 1-3
| [\xF1-\xF3][\x80-\xBF] # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF] # plane 16
)*$%xs’ , $string ) ) <
return $string ;
> else <
return iconv ( ‘CP1252’ , ‘UTF-8’ , $string );
>
>
?>
and assign in this way:
$x = to_utf8 ( $_GET [ ‘myvar’ ] );
?>
$x store: in the first case «º» (good) and in the second case «º» (good)
Solve a lot of i18n problems.
Please fix the auto-urldecode of $_GET var in the next PHP version.
Using PHP urlencode and urldecode
Monty Shokeen Last updated Aug 21, 2021
Every now and then you will have to pass data to a webpage or service using a URL, for example in a GET request. This is pretty easy, since URLs are basically just text strings, but things can sometimes get complicated. For example, some characters have a special meaning in URLs (like : ), and some characters aren’t allowed at all (like space). Sometimes you even need to encode a URL in another URL!
In this tutorial, you’ll learn why you need to encode or decode strings to be passed in URLs and how to use the built-in PHP functions to do so.
Why Encode and Decode Strings to URLs
Perhaps you want to pass some info as query parameters to a web service or another web page. Say, for example, you want to send the following data to a website as a query string:
key | data |
redirect | https://code.tutsplus.com |
author | monty shokeen |
page | 2 |
That information would be encoded in the following query string:
https://www.example.com?redirect=https%3A%2F%2Fcode.tutsplus.com&author=monty%20shokeen&page=2
Notice that the special characters like : and / in the «redirect» URL have been encoded as %3A and %2F to avoid interfering with the structure of the overall URL. This is called escaping, and that’s where the encoding functions come in.
The server at example.com will receive that encoded information in the query string and will probably need to decode it later. That’s where URL decoding is important.
Encoding Strings With urlencode() and rawurlencode()
There are two different functions in PHP for encoding strings in URLs. These are called urlencode() and rawurlencode() . The major difference between these two is the set of characters they encode and how they handle spaces.
In the case of urlencode() , the function replaces all other non-alphanumeric characters except — , _ and . with a percent sign followed by two hex digits. Any spaces are replaced with the + character. On the other hand, the rawurlencode() function replaces all other non-alphanumeric characters except — , _ , . , and ~ with a percent sign followed by two hex digits. This function also replaces spaces with a percent followed by two hex digits: %20 .
The following example should clear things up a bit for you.
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:
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:
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:
Types of URL Encoding and Decoding in PHP
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].