note 109468 deleted from function.urlencode by danbrown

From: Date: Fri, 20 Jul 2012 15:24:58 +0000
Subject: note 109468 deleted from function.urlencode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189330@lists.php.net to get a copy of this message
Note Submitter: bart at bartbosma dot eu ---- In response to LaKing at D250 dot hu, this is incredible bad practice. You should obviously never spam str_replace when you could instead do it like this: $array1[]="%C3%8D"; $array1[]="%C3%AD"; $array2[]="%CD"; $array2[]=%ED"; $string=str_replace($array1, $array2, $string); By the way, here is a function i wrote, (more on my blog): if (!function_exists("filesystemcode")){ function filesystemcode($string, $arr1="a", $arr2="b") { $c=array(); if (isset($arr1)) { if (!isset(${$arr1}) || !is_array(${$arr1})){ ${$arr1} = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D'); }} if (isset($arr2)) { if (!isset(${$arr2}) || !is_array(${$arr2})){ ${$arr2} = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");}else{$b='';} } if (isset($a) && isset($b)){ return str_replace($a, $b, $string); }else{return($string);} } } Remove html compatible encoded illegal filename characters: //$string="%21dad"; $string=filesystemcode($string, "a", "c"); //echo $string; //"dad"; Remove illegal filename chars: //$string="/dad"; $string=filesystemcode($string, "b", "c"); //echo $string; //"dad"; Convert filesystem incompatible chars into html compatible chars: //$string="!dad"; $string=filesystemcode($string, "b", "a"); //echo $string; //"%21dad"; Convert html compatible chars into filesystem incompatible chars: //$string="%21dad"; $string=filesystemcode($string, "a", "b"); //echo $string; //"!dad";

« previous php.notes (#189330) next »