What is the equivalent of JavaScript's encodeURIcomponent in PHP?
+2
A:
Try rawurlencode
. Or to be more precise:
function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
This function works exactly how encodeURIComponent
is defined:
encodeURIComponent
escapes all characters except the following: alphabetic, decimal digits,-
_
.
!
~
*
'
(
)
Gumbo
2009-11-14 13:39:20
Thank you!I thought I might be able to do it without a function, but ok.
sombe
2009-11-14 13:50:59
@Gal: You will only need that function if you need an identical output.
Gumbo
2009-11-14 13:57:44
Nice, you can see the comparison between functions here: http://devpro.it/examples/php_js_escaping.php
philfreo
2010-09-20 17:04:52
yes. the problem is that it's not entirely like encodeURI, it converts every character, even ^I wanted something that would function the exact same way without me having to intervene ^^.
sombe
2009-11-14 13:47:19
A:
Catwashere
2010-06-02 19:04:19