note 96394 modified in function.urlencode by danbrown

From: Date: Wed, 24 Feb 2010 22:13:12 +0000
Subject: note 96394 modified in function.urlencode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166342@lists.php.net to get a copy of this message
I needed a function in PHP to do the same job as the complete escape function in Javascript. It took me some time not to find it. But findaly I decided to write my own code. So just to save time: <?php function fullescape($in) { $out = ''; for ($i=0;$i<strlen($in);$i++) { $hex = dechex(ord($in[$i])); if ($hex=='') $out = $out.urlencode($in[$i]); else $out = $out .'%'.((strlen($hex)==1) ? ('0'.strtoupper($hex)):(strtoupper($hex))); } $out = str_replace('+','%20',$out); $out = str_replace('_','%5F',$out); $out = str_replace('.','%2E',$out); $out = str_replace('-','%2D',$out); return $out; } ?> It can be fully decoded using the unscape function in Javascript. --was-- I needed a function in PHP to do the same job as the complete escape function in Javascript. It took me some time not to find it. But findaly I decided to write my own code. So just to save time: function fullescape($in) { $out = ''; for ($i=0;$i<strlen($in);$i++) { $hex = dechex(ord($in[$i])); if ($hex=='') $out = $out.urlencode($in[$i]); else $out = $out .'%'.((strlen($hex)==1) ? ('0'.strtoupper($hex)):(strtoupper($hex))); } $out = str_replace('+','%20',$out); $out = str_replace('_','%5F',$out); $out = str_replace('.','%2E',$out); $out = str_replace('-','%2D',$out); return $out; } It can be fully decoded using the unscape function in Javascript. http://php.net/manual/en/function.urlencode.php

« previous php.notes (#166342) next »