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

Share:
41,316
neonant
Author by

neonant

Updated on August 07, 2020

Comments

  • neonant
    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's decodeURIComponent()?

  • neonant
    neonant over 13 years
    i have used $_POST for fetching,but urldecode() doesnt work on unicode characters.
  • Explosion Pills
    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
    CJT3 about 11 years
    @ExplosionPills was that double semicolon at the end a typo?
  • Matt K
    Matt K over 7 years
    Side 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 years
    Be careful: urldecode('+') === ' ' and decodeURIComponent('+') === '+'. Currently dont know why.