What is the equivalent of JavaScript's decodeURIcomponent in PHP?
Asked Answered
C

2

49

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()?

Callous answered 9/10, 2010 at 8:26 Comment(0)
R
73
urldecode()

However you do not need to use it on $_REQUEST variables, which are already decoded automatically.

Reprehend answered 9/10, 2010 at 8:28 Comment(5)
i have used $_POST for fetching,but urldecode() doesnt work on unicode characters.Callous
<?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');; } ?>Reprehend
@ExplosionPills was that double semicolon at the end a typo?Cheyney
Side note: I am using encodeURIComponent() to pass data to a Laravel application and the request doesn't seem to automagically decode the value.Ingra
Be careful: urldecode('+') === ' ' and decodeURIComponent('+') === '+'. Currently dont know why.Tracitracie
C
7

rawurldecode() which does not decode plus to space charactor

Craniology answered 6/8, 2020 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.