What is the equivalent of JavaScript's decodeURIcomponent in PHP?
41,316
Solution 1
urldecode()
However you do not need to use it on $_REQUEST variables, which are already decoded automatically.
Solution 2
rawurldecode()
which does not decode plus to space charactor
Author by
neonant
Updated on August 07, 2020Comments
-
neonant almost 4 years
I have a string with unicode characters that I am transferring via HTTP. This string was encoded with Javascript's
encodeURIcomponent()
. Is there an equivalent function in php to Javascript'sdecodeURIComponent()
? -
neonant over 13 yearsi have used $_POST for fetching,but urldecode() doesnt work on unicode characters.
-
Explosion Pills over 13 years<?php function utf8_urldecode($str) { $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str)); return html_entity_decode($str,null,'UTF-8');; } ?>
-
CJT3 about 11 years@ExplosionPills was that double semicolon at the end a typo?
-
Matt K over 7 yearsSide note: I am using
encodeURIComponent()
to pass data to a Laravel application and the request doesn't seem to automagically decode the value. -
Илья Зеленько almost 4 yearsBe careful:
urldecode('+') === ' '
anddecodeURIComponent('+') === '+'
. Currently dont know why.