- rawurldecode
- Parameters
- Return Values
- Examples
- Notes
- See Also
- User Contributed Notes 8 notes
- 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
- Using PHP urlencode and urldecode
- Why Encode and Decode Strings to URLs
- Encoding Strings With urlencode() and rawurlencode()
- PHP URL Decode example
- Share on social media
- URLDecoder
- How to decode/unescape a url encoded string in Ruby
- URL Decoding in Java
- How to Decode URI components in Javascript
- URL Decoding query strings or form parameters in Python
- How to perform URL decoding in Golang
rawurldecode
Returns a string in which the sequences with percent ( % ) signs followed by two hex digits have been replaced with literal characters.
Parameters
Return Values
Returns the decoded URL, as a string.
Examples
Example #1 rawurldecode() example
echo rawurldecode ( ‘foo%20bar%40baz’ ); // foo bar@baz
Notes
Note:
rawurldecode() does not decode plus symbols (‘+’) into spaces. urldecode() does.
See Also
- rawurlencode() — URL-encode according to RFC 3986
- urldecode() — Decodes URL-encoded string
- urlencode() — URL-encodes string
- » RFC 3986
User Contributed Notes 8 notes
To sum it up: the only difference of this function to the urldecode function is that the «+» character won’t get translated.
Hi everybody =) My name is Javier and I’m from Argentina.
I’ve had a little issue with latin characters like ñ»,»Ñ»,»á»,»é»,»í», etc.
They are not decoded with rawurlencode(), so I’ve made this:
function urlRawDecode ( $raw_url_encoded )
# Hex conversion table
$hex_table = array(
0 => 0x00 ,
1 => 0x01 ,
2 => 0x02 ,
3 => 0x03 ,
4 => 0x04 ,
5 => 0x05 ,
6 => 0x06 ,
7 => 0x07 ,
8 => 0x08 ,
9 => 0x09 ,
«A» => 0x0a ,
«B» => 0x0b ,
«C» => 0x0c ,
«D» => 0x0d ,
«E» => 0x0e ,
«F» => 0x0f
);
# Fixin’ latin character problem
if( preg_match_all ( «/\%C3\%([A-Z0-9])/i» , $raw_url_encoded , $res ))
$res = array_unique ( $res = $res [ 1 ]);
$arr_unicoded = array();
foreach( $res as $key => $value ) $arr_unicoded [] = chr (
( 0xc0 | ( $hex_table [ substr ( $value , 0 , 1 )] | ( 0x03 & $hex_table [ substr ( $value , 1 , 1 )])
);
$res [ $key ] = «%C3%» . $value ;
>
$raw_url_encoded = str_replace (
$res ,
$arr_unicoded ,
$raw_url_encoded
);
>
# Return decoded raw url encoded data
return rawurldecode ( $raw_url_encoded );
>
print urlRawDecode ( «%C3%A1%C3%B1» );
?>
For example, you have the character «ñ» encoded like this «%C3%B1».
This is nothing more and nothing less than 0xc3 and 0xb1,
they are binary numbers, (HHHH LLLL, where HHHH=High and LLLL=Low).
0xc3 = 1100 0011 (binary 8 bit word), 0xb1 = 1011 0001 (binary 8 bit word),
To convert a raw encoded character to ascii we have to make boolean operations
between this two operands (0xc3 and 0xb1), boolean algebra were defined by George
Boole, we need to use them here. The first one we going to use is the
logical OR («|» or «pipe») and logical AND («&» or «and person»).
A logical OR implies the following truth table:
a b (a OR b)
0 0 0
0 1 1 (a OR b or Both, a and b, must be true to get a true result)
1 0 1
1 1 1
A logical AND implies the following truth table:
a b (a AND b)
0 0 0
0 1 0
1 0 0
1 1 1 (Both a AND b, must be true to get a true result)
So, here we have to make a logical OR with both 0xc3 and 0xb1 HIGH nibble,
a nibble is a half byte (4 bits), so we have to make a logical OR between
1100 (0xc) and 1011 (0xb), we going to get this: 1111 (0xf), then we have to make
a logical AND between both LOW nibble, 0011 (0x3) and 0001 (0x1), we going to get
this: 0001, so, if we want to see the final result, we have to put HIGH and LOW
nibble on his Byte position, like this: 1111 0001 (0xf1) and that is nothing
more and nothing less than «ñ» (to check this out, try the following: print(chr(0xf1));).
# Conversion example %C3%B1 to ASCII (0x71)
print(
chr (
( 0xc0 | 0x0b )
);
// 1100 0000 OR 1011 0000 = 1111 0000 (0xf0)
// 0000 0011 AND 0000 0001 = 0000 0001 (0x01)
// 1111 0000 OR 0000 0001 = 1111 0001 (0xf1)
?>
PS: I’m so sorry about my english, I know, is horrible 😛
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
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.
PHP URL Decode example
PHP contains two inbuilt functions called urldecode() and rawurldecode() to decode a URL encoded string back to its normal form.
# Syntax urldecode (string $str) : string
The urldecode() function takes a URL encoded (percent encoded) string and decodes it. It is just the opposite of urlencode() function. It converts plus sign ( + ) to space character.
echo urldecode("Hell%C3%B6+W%C3%B6rld") . "\n"; ?>
The rawurldecode() function
The rawurldecode() function is just like urldecode() function except that it doesn’t decode plus sign ( + ) to space character —
# Syntax rawurldecode (string $str) : string
echo rawurldecode("Hell%C3%B6+W%C3%B6rld") . "\n"; ?>
Share on social media
URLDecoder
Use our free online tool to decoder any URL encoded string back to its normal form.
How to decode/unescape a url encoded string in Ruby
Ruby URL Decoding example. Learn How to decode a URL encoded string in Ruby. Ruby’s CGI::Unescape method can be used to unescape or decode any url encoded string.
URL Decoding in Java
Java URL Decoding example. Learn how to decode any URL encoded query string or form parameter in Java. Java Provides a URLDecoder class containing a method named decode(). It takes a URL encoded string and a character encoding as arguments and decodes the string using the supplied encoding.
How to Decode URI components in Javascript
Javascript Url Decoding example. Learn how to decode URI components in Javascript. You can decode URI components in Javascript using the decodeURIComponent() function. It performs the inverse operation of encodeURIComponent(). It uses UTF-8 encoding scheme to decode URI components.
URL Decoding query strings or form parameters in Python
Python URL Decoding example. Learn How to decode URLs in Python. URL decoding, as the name suggests, is the inverse operation of URL encoding. It is often needed when you’re reading query strings or form parameters received from a client.
How to perform URL decoding in Golang
Golang Url Decoding example. In this article, you’ll learn how to URL decode query strings or form parameters in Golang. URL Decoding is the inverse operation of URL encoding. It converts the encoded characters back to their normal form.