{"id":1631,"date":"2017-03-22T15:10:06","date_gmt":"2017-03-22T09:40:06","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1631"},"modified":"2017-03-28T19:11:05","modified_gmt":"2017-03-28T13:41:05","slug":"php-urlencode-vs-rawurlencode","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-urlencode-vs-rawurlencode\/","title":{"rendered":"PHP &#8211; urlencode vs rawurlencode"},"content":{"rendered":"<h4 id=\"urlencode\"><span style=\"color: #ff6600;\"><strong>urlencode<\/strong><\/span><\/h4>\n<ul>\n<li>urlencode \u2014 URL-encodes string<\/li>\n<\/ul>\n<h4 id=\"description\"><span style=\"color: #808000;\"><b>Description<\/b><\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dstring%20urlencode%20(%20string%20%24str%20)%0A\u201d message=\u201dphp  code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>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.<\/li>\n<\/ul>\n<h4 id=\"example-1-urlencode-example\"><span style=\"color: #800080;\"><b>Example #1 <\/b><b>urlencode<\/b><b>() example<\/b><\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Aecho%20\u2019%3Ca%20href%3D%22mycgi%3Ffoo%3D\u2019%2C%20urlencode(%24userinput)%2C%20\u2019%22%3E\u2019%3B%0A%3F%3E%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"rawurlencode\"><span style=\"color: #ff6600;\"><strong>rawurlencode<\/strong><\/span><\/h4>\n<ul>\n<li>rawurlencode \u2014 URL-encode according to RFC 3986<\/li>\n<\/ul>\n<h4 id=\"description-2\"><span style=\"color: #808000;\"><b>Description<\/b><\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%20%20string%20rawurlencode%20(%20string%20%24str%20)%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Encodes the given string according to \u00bb RFC 3986.<\/li>\n<\/ul>\n<h4 id=\"example-1-including-a-password-in-an-ftp-url\"><span style=\"color: #800080;\"><b>Example #1 including a password in an FTP URL<\/b><\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Aecho%20\u2019%3Ca%20href%3D%22ftp%3A%2F%2Fuser%3A\u2019%2C%20rawurlencode(\u2018foo%20%40%2B%25%2F\u2019)%2C%0A%20%20%20%20%20\u2019%40ftp.example.com%2Fx.txt%22%3E\u2019%3B%0A%3F%3E%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h3 id=\"urlencode-vs-rawurlencode\"><span style=\"color: #ff6600;\">urlencode vs rawurlencode<\/span><\/h3>\n<p>The main difference between the two is the encoding of a [SPACE]\n<ul>\n<li><strong>URLENCODE<\/strong> \u2013 encodes a space as a plus sign \u2018<b>+<\/b>\u2018<\/li>\n<li><strong>RAWURLENCODE<\/strong> \u2013 encodes a space as \u2018%20\u2019<\/li>\n<\/ul>\n<h4 id=\"rawurlencode-2\"><span style=\"color: #808000;\"><b>rawurlencode:\u00a0<\/b><\/span><\/h4>\n<ul>\n<li>\u00a0the follows RFC 1738 prior to PHP 5.3.0 and RFC 3986 afterwards.<\/li>\n<\/ul>\n<ul>\n<li>Returns a string in which all non-alphanumeric characters except -_.~ have been replaced with a percent (%) sign followed by two hex digits.<\/li>\n<\/ul>\n<ul>\n<li>This is the encoding described in \u00bb RFC 3986 for protecting literal characters from being interpreted as special URL delimiters, and for protecting URLs from being mangled by transmission media with character conversions (like some email systems).<\/li>\n<\/ul>\n<ul>\n<li>Note on RFC 3986 vs 1738. rawurlencode prior to php 5.3 encoded the tilde character (~) according to RFC 1738.<\/li>\n<\/ul>\n<ul>\n<li>As of PHP 5.3, however, rawurlencode follows RFC 3986 which does not require encoding tilde characters.<\/li>\n<\/ul>\n<h4 id=\"urlencode-2\"><span style=\"color: #800080;\"><b>urlencode<\/b>\u00a0:<\/span><\/h4>\n<ul>\n<li>urlencode encodes spaces as plus signs (not as %20 as done in rawurlencode)<\/li>\n<\/ul>\n<ul>\n<li>Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.<\/li>\n<\/ul>\n<ul>\n<li>It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application\/x-www-form-urlencoded media type.<\/li>\n<\/ul>\n<ul>\n<li>This differs from the \u00bb RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.<\/li>\n<\/ul>\n<h4 id=\"differences-in-ebcdic\"><span style=\"color: #ff6600;\"><strong>Differences in EBCDIC:<\/strong><\/span><\/h4>\n<h4 id=\"urlencode-3\"><span style=\"color: #808000;\"><b>URLENCODE:<\/b><\/span><\/h4>\n<ul>\n<li>Same iteration setup as with ASCII<\/li>\n<\/ul>\n<ul>\n<li>Still translating the \u201cspace\u201d character to a + sign. Note\u2013 I think this needs to be compiled in EBCDIC or you\u2019ll end up with a bug? Can someone edit and confirm this?<\/li>\n<\/ul>\n<ul>\n<li>It checks if the present char is a char before 0, with the exception of being a . or -, OR less than A but greater than char 9, OR greater than Z and less than a but not a _. OR greater than z . If it matches any of those, do a similar lookup as found in the ASCII version.<\/li>\n<\/ul>\n<h4 id=\"rawurlencode-3\"><span style=\"color: #800080;\"><b>RAWURLENCODE:<\/b><\/span><\/h4>\n<ul>\n<li>Same iteration setup as with ASCII<\/li>\n<\/ul>\n<ul>\n<li>Same check as described in the EBCDIC version of URL Encode, with the exception that if it\u2019s greater than z, it excludes ~ from the URL encode.<\/li>\n<\/ul>\n<ul>\n<li>Same assignment as the ASCII RawUrlEncode<\/li>\n<\/ul>\n<ul>\n<li>Still appending the \\0 byte to the string before return.<\/li>\n<\/ul>\n<p><span style=\"color: #ff6600;\"><b>One practical reason to choose one over the other is if you\u2019re going to use the result in another environment, for example JavaScript.<\/b><\/span><\/p>\n<ul>\n<li>In PHP urlencode(\u2018test 1\u2019) returns \u2018test+1\u2019 while rawurlencode(\u2018test 1\u2019) returns \u2018test%201\u2019 as result.<\/li>\n<\/ul>\n<ul>\n<li>But if you need to \u201cdecode\u201d this in JavaScript using decodeURI() function then decode URI(\u201ctest+1\u201d) will give you \u201ctest+1\u201d while decode URI(\u201ctest%201\u201d) will give you \u201ctest 1\u201d as result.<\/li>\n<\/ul>\n<ul>\n<li>In other words the space (\u201d \u201c) encoded by urlencode to plus (\u201c+\u201d) in PHP will not be properly decoded by decode URI in JavaScript.<\/li>\n<\/ul>\n<ul>\n<li>In such cases the rawurlencode PHP function should be used.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>urlencode urlencode \u2014 URL-encodes string Description [pastacode lang=\u201dphp\u201d manual=\u201dstring%20urlencode%20(%20string%20%24str%20)%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] 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. Example #1 urlencode() example [pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Aecho%20\u2019%3Ca%20href%3D%22mycgi%3Ffoo%3D\u2019%2C%20urlencode(%24userinput)%2C%20\u2019%22%3E\u2019%3B%0A%3F%3E%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] rawurlencode rawurlencode [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[3386,3401,3399,3400,3389,3402,3398,3395,3397,3396,3388,3393,3391,3392,3385,3394,3387,3390],"class_list":["post-1631","post","type-post","status-publish","format-standard","hentry","category-php","tag-in-asp-net","tag-php-rawurldecode","tag-php-urlencode-array","tag-php-urlencode-example","tag-php-urlencode-for-chinese-characters","tag-php-urlencode-online","tag-php-urlencode-space","tag-rawurlencode-equivalent-in-android","tag-rawurlencode-javascript","tag-rawurlencode-online","tag-rawurlencode-and-urlencode-not-working-in-codeigniter","tag-substituting-whitespaces-with-in-php-urlencode-and-rawurlencode-does-not-work","tag-urlencode-to-lower-case-in-php","tag-urlencoderawurlencode-and-automatic-decoding","tag-urlencoding-in-a-url-problem","tag-urlencoding-just-for-arabic-text-in-url-not-for-whole-url","tag-why-is-there-urlencode-and-urlpathencode","tag-wrong-urlencoding"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1631"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1631\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}