How to encode URL parameters?

With PHP echo urlencode(“http://www.image.com/?username=unknown&password=unknown”); Result http%3A%2F%2Fwww.image.com%2F%3Fusername%3Dunknown%26password%3Dunknown With Javascript: var myUrl = “http://www.image.com/?username=unknown&password=unknown”; var encodedURL= “http://www.foobar.com/foo?imageurl=” + encodeURIComponent(myUrl); DEMO: http://jsfiddle.net/Lpv53/

What is the equivalent of JavaScript’s encodeURIcomponent in PHP?

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, – _ . ! ~ * ‘ ( )