How Does Urlencode Work In Php Urlencode() Function, Using Php Urlencode And Urldecode

I"m making a search page, where you type a tìm kiếm query và the khung is submitted lớn search.php?query=your query. What PHP function is the best và that I should use for encoding/decoding the search query?


*

*

Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
For the URI query use urlencode/urldecode; for anything else use rawurlencode/rawurldecode.

Bạn đang xem: How does urlencode work in php urlencode() function, using php urlencode and urldecode

The difference between urlencode and rawurlencode is that


*

*

The cunningly-named urlencode() & urldecode().

However, you shouldn"t need to use urldecode() on variables that appear in $_POST và $_GET.


*

Here is my use case, which requires an exceptional amount of encoding. Maybe you think it is contrived, but we run this in production. Coincidently, this covers every type of encoding, so I"m posting as a tutorial.

Use case description

Somebody just bought a prepaid gift card ("token") on our website. Tokens have corresponding URLs khổng lồ redeem them. This customer wants to thư điện tử the URL to lớn someone else. Our web page includes a mailto links that lets them vì chưng that.

PHP code

// The order system generates some opaque token$token = "w%a&!e#"^2(^
azW";// Here is a URL khổng lồ redeem that token$redeemUrl = "https://httpbin.org/get?token=" . Urlencode($token);// Actual contents we want for the email$subject = "I just bought this for you";$body = "Please enter your shipping details here: " . $redeemUrl;// A URI for the e-mail as prescribed$mailToUri = "mailto:?subject=" . Rawurlencode($subject) . "&body=" . Rawurlencode($body);// Print an HTML element with that mailto linkecho "Email your friend";Note: the above assumes you are outputting to lớn a text/html document. If your output truyền thông type is text/json then simply use $retval<"url"> = $mailToUri; because đầu ra encoding is handled by json_encode().

Test case

Run the code on a PHP demo site (is there a canonical one I should mention here?)Click the linkSend the emailGet the emailClick that link

You should see:

"args": "token": "w%a&!e#"^2(^
azW", and of course this is the JSON representation of $token above.

Xem thêm: phần mềm hs 2018


mô tả
edited Dec 2, 2022 at 22:38
Peter Mortensen
30.8k2121 gold badges104104 silver badges125125 bronze badges
answered Jan 15, 2018 at 17:14
William EntrikenWilliam Entriken
35.7k2323 gold badges141141 silver badges189189 bronze badges
1
|
4
You can use URL encoding functions. PHP has the

rawurlencode()function.

ASP.NET has the

Server.URLEncode()function.

In JavaScript, you can use the

encodeURIComponent()function.


share
edited Dec 2, 2022 at 22:41
Peter Mortensen
30.8k2121 gold badges104104 silver badges125125 bronze badges
answered Apr 29, 2019 at 12:03
Amranur RahmanAmranur Rahman
1,0071919 silver badges2727 bronze badges
|
1
Based on what type of RFC standard encoding you want to lớn perform or if you need to lớn customize your encoding you might want khổng lồ create your own class.

/** * UrlEncoder make it easy khổng lồ encode your URL */class UrlEncoder public const STANDARD_RFC1738 = 1; public const STANDARD_RFC3986 = 2; public const STANDARD_CUSTOM_RFC3986_ISH = 3; // địa chỉ cửa hàng more here static function encode($string, $rfc) switch ($rfc) case self::STANDARD_RFC1738: return urlencode($string); break; case self::STANDARD_RFC3986: return rawurlencode($string); break; case self::STANDARD_CUSTOM_RFC3986_ISH: // địa chỉ your custom encoding $entities = <"%21", "%2A", "%27", "%28", "%29", "%3B", "%3A", "%40", "%26", "%3D", "%2B", "%24", "%2C", "%2F", "%3F", "%25", "%23", "%5B", "%5D">; $replacements = <"!", "*", """, "(", ")", ";", ":", "
", "&", "=", "+", "$", ",", "/", "?", "%", "#", "<", ">">; return str_replace($entities, $replacements, urlencode($string)); break; default: throw new Exception("Invalid RFC encoder - See class const for reference"); break; Use example:

$dataString = "https://www.google.pl/search?q=PHP is **great**!&id=123&css=#kolo&email=me
liszka.com)";$dataStringUrlEncodedRFC1738 = UrlEncoder::encode($dataString, UrlEncoder::STANDARD_RFC1738);$dataStringUrlEncodedRFC3986 = UrlEncoder::encode($dataString, UrlEncoder::STANDARD_RFC3986);$dataStringUrlEncodedCutom = UrlEncoder::encode($dataString, UrlEncoder::STANDARD_CUSTOM_RFC3986_ISH);Will output:

string(126) "https%3A%2F%2Fwww.google.pl%2Fsearch%3Fq%3DPHP+is+%2A%2Agreat%2A%2A%21%26id%3D123%26css%3D%23kolo%26email%3Dme%40liszka.com%29"string(130) "https%3A%2F%2Fwww.google.pl%2Fsearch%3Fq%3DPHP%20is%20%2A%2Agreat%2A%2A%21%26id%3D123%26css%3D%23kolo%26email%3Dme%40liszka.com%29"string(86) "https://www.google.pl/search?q=PHP+is+**great**!&id=123&css=#kolo&email=me
liszka.com)"* Find out more about RFC standards:https://datatracker.ietf.org/doc/rfc3986/and urlencode vs rawurlencode?


cốt truyện
answered Aug 23, 2020 at 1:37
DevWLDevWL
16.2k66 gold badges8888 silver badges8383 bronze badges
|
1
You know how people keep saying things like: "Never manually craft a JSON string in PHP -- always call json_encode() for stability/reliability."?

Well, if you are building a query string, then I say: "Never manually craft a URL query string in PHP—always hotline http_build_query() for stability/reliability."

Demo:

$array = < "query" => "your query", "example" => null, "Qbert says:" => "&%=#?/">;echo http_build_query($array);echo " --- ";echo http_build_query($array, "", "&");Output:

query=your+query&Qbert+says%3A=%26%25%3D%23%3F%2F---query=your+query&Qbert+says%3A=%26%25%3D%23%3F%2FThe fine print on this function is that if an element in the đầu vào array has a null value, then that element will not be included in the output đầu ra string.

Here is an educational answer on the Joomla Stack Exchange site which encourages the use of & as the custom delimiter: Why are Joomla URL query strings commonly delimited with "&" instead of "&"?

Initially packaging your query string data in array size offers a compact & readable structure, then the điện thoại tư vấn of http_build_query() does the hard work và can prevent data corruption. I generally opt for this technique even for small query string construction.