Php decode url parameters

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.

Читайте также:  Jquery css добавить стиль

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].

Источник

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.

Источник

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:

  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

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

Источник

Оцените статью