php decode url params
PHP URL Decode example
PHP contains two inbuilt functions called urldecode() and rawurldecode() to decode a URL encoded string back to its normal form.
The urldecode() function
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.
Example
The rawurldecode() function
Example
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.
urlencode
(PHP 4, PHP 5, PHP 7, PHP 8)
urlencode — URL-encodes string
Description
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
Parameters
The string to be encoded.
Return Values
Examples
Example #1 urlencode() example
Example #2 urlencode() and htmlentities() example
Notes
Be careful about variables that may match HTML entities. Things like &, © and £ are parsed by the browser and the actual entity is used instead of the desired variable name. This is an obvious hassle that the W3C has been telling people about for years. The reference is here: » http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2.
See Also
User Contributed Notes 25 notes
urlencode function and rawurlencode are mostly based on RFC 1738.
However, since 2005 the current RFC in use for URIs standard is RFC 3986.
Here is a function to encode URLs according to RFC 3986.
I needed encoding and decoding for UTF8 urls, I came up with these very simple fuctions. Hope this helps!
Don’t use urlencode() or urldecode() if the text includes an email address, as it destroys the «+» character, a perfectly valid email address character.
Unless you’re certain that you won’t be encoding email addresses AND you need the readability provided by the non-standard «+» usage, instead always use use rawurlencode() or rawurldecode().
I needed a function in PHP to do the same job as the complete escape function in Javascript. It took me some time not to find it. But findaly I decided to write my own code. So just to save time:
urlencode is useful when using certain URL shortener services.
The returned URL from the shortener may be truncated if not encoded. Ensure the URL is encoded before passing it to a shortener.
(tilde), while urlencode does.
Below is our jsonform source code in mongo db which consists a lot of double quotes. we are able to pass this source code to the ajax form submit function by using php urlencode :
If you want to pass a url with parameters as a value IN a url AND through a javascript function, such as.
. pass the url value through the PHP urlencode() function twice, like this.
However, some weird things happen when dealing with characters like (these are HTML entities): ‼ ▐ ┐and Θ have weird things going on.
If you try to pass one in Internet Explorer, IE will *disable* the submit button. Firefox, however, does something weirder: it will convert it to it’s HTML entity. It will display properly, but only when you don’t convert entities.
The point? Be careful with decorative characters.
This very simple function makes an valid parameters part of an URL, to me it looks like several of the other versions here are decoding wrongly as they do not convert & seperating the variables into &.
$vars=array(‘name’ => ‘tore’,’action’ => ‘sell&buy’);
echo MakeRequestUrl($vars);
Will output: action=sell%26buy&name=tore
Constructing hyperlinks safely HOW-TO:
= ‘machine/generated/part’ ;
$url_parameter1 = ‘this is a string’ ;
$url_parameter2 = ‘special/weird «$characters»‘ ;
$link_label = «Click here & you’ll be » ;
Shortly:
— Use urlencode for all GET parameters (things that come after each «=»).
— Use rawurlencode for parts that come before «?».
— Use htmlspecialchars for HTML tag parameters and HTML text content.
look on index.php
array (size=0)
empty
test-bla-bla-4%253E2-y-3%253C6
look on test-bla-bla-4%253E2-y-3%253C6
array (size=1)
‘token’ => string ‘bla-bla-4>2-y-3
Simple static class for array URL encoding
/**
*
* URL Encoding class
* Use : urlencode_array::go() as function
*
*/
class urlencode_array
<
URL Encoding and Decoding with PHP
PHP Tutorials
Operators
Control Structures
Loops
PHP Forms
PHP Advanced
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
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].
rawurldecode
(PHP 4, PHP 5, PHP 7, PHP 8)
rawurldecode — Decode URL-encoded strings
Description
Returns a string in which the sequences with percent ( % ) signs followed by two hex digits have been replaced with literal characters.
Parameters
The URL to be decoded.
Return Values
Returns the decoded URL, as a string.
Examples
Example #1 rawurldecode() example
echo rawurldecode ( ‘foo%20bar%40baz’ ); // foo bar@baz
Notes
rawurldecode() does not decode plus symbols (‘+’) into spaces. urldecode() does.
See Also
User Contributed Notes 7 notes
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));).
This » # Conversion example %C3%B1 to ASCII (0x71)
print(
chr (
( 0xc0 | 0x0b 4 ) | ( 0x03 & 0x01 )
)
);
// 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 😛
urldecode
urldecode — Декодирование URL-кодированной строки
Описание
Декодирует любые % ## кодированные последовательности в данной строке. Символ «плюс» (‘+‘) декодируется в символ пробела.
Список параметров
Строка, которая должны быть декодирована.
Возвращаемые значения
Возвращает декодированную строку.
Примеры
Пример #1 Пример использования urldecode()
Примечания
Смотрите также
Коментарии
This seems to decode correctly between most browsers and charater coding configurations. Specially indicated for direct parsing of URL as it comes on environment variables:
function crossUrlDecode($source) <
$decodedStr = »;
$pos = 0;
$len = strlen($source);
For compatibility of new and old brousers:
function unicode_decode($txt) <
$txt = ereg_replace(‘%u0([[:alnum:]]<3>)’, ‘&#x\1;’,$txt);
$txt = ereg_replace(‘%([[:alnum:]]<2>)’, ‘&#x\1;’,$txt);
return ($txt);
>
If you have a «html reserved word» as variable name (i.e. «reg_var») and you pass it as an argument you will get a wrong url. i.e.
you will get a wrong url like this
Simply add a space between «&» and «reg_var» and it will work!
urldecode does not decode «%0» bypassing it. I can cause troble when you are working with fixed lenght strings.
You can you the function below.
$array = split («%»,$string);
nataniel, your function needs to be corrected as follows:
since some codes does not begin with %u0.
header(‘Content-type: text/html; charset=UTF-8’);
This «common error» is one of the underlying causes of the Santy.A worm which affects phpBB
About reg_var and «html reserved words»
Do not add spaces as the user suggests.
Instead, do what all HTML standards says and encode & in URLs as & in your HTML.
Note that the decoding of & to & is done in the browser, and it’s done right after splitting the HTML into tags, attributes and content, but it works both for attributes and content.
This mean you should &entitify all &-s in any other HTML attributes as well, such as in a form with
.
This function doesn’t decode unicode characters. I wrote a function that does.
Finally, i’ve found a simple solution (only 3 lines of code) that does it (at least in my configuration):
It’s worth pointing out that if you are using AJAX and need to encode strings that are being sent to a PHP application, you may not need to decode them in PHP.
And is sent with an AJAX POST request with the header:
ajaxVar.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’)
mkaganer at gmail dot com:
try using encodeURI() instead of encode() in javascript. That worked for me, while your solution did not on __some__ national characters (at least in IE6).
Send json to PHP via AJAX (POST)
After this, you can do json_decode on your string.
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)
To fix that, you can use this function:
Solve a lot of i18n problems.
if the content type is multipart/form-data. the data remains un-decoded. and we have to manually handle the decoding at our end
?>
For some reason, a variable with an ampersand would stay encoded while other POST variables were automatically decoded. I concatenated data from an html form before submitting, in case you wish to know what happened on the browser end.