note 109978 deleted from function.explode by danbrown

From: Date: Wed, 12 Sep 2012 17:31:39 +0000
Subject: note 109978 deleted from function.explode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190670@lists.php.net to get a copy of this message
Note Submitter: exadra37 at nospam dot asualojaonline dot com ---- After read the previous note from @kkobashi at kobashicomputing dot com i have made this function to handle better the explode function. Sorry for my English, but my natural Language is Portuguese... <?php /** * This function will use php explode to split a string into a array without null or false values. * If the provided delimeter will not find in string it will give an error and return false. * @param srting $delimeter - the delimiter to use in order to separate the elements from the string. Ex: "-". * @param string $string - the string to be separated by the delimeter into a array. Ex: "will-found-delimiter"; "not/found/delimeter" * @param integer $limit - the limit off fields in the exploded array. If no value was passed will assume by default 100. * The above example will achieve 3 fields in array. * * @return boolean false using $string="not/found/delimeter" or $explode_array using $string="will-found-delimiter" and $delimiter="-" * * @author exadra37 */ function Explode_String($delimeter, $string, $limit = 100) { // using in_array to see if the $string is in the result array from explode. If true is because the delimeter was not found in the $string. // using array_filter to remove all null and false values from the string. When remove some value from the arrau will keep the same index key. // using explode to split the $string into $explode_array. if (in_array($string,($explode_array = array_filter(explode($delimeter, $string, $limit), "explode_filtered_empty")))) { // handle here the error message. You must change this line according to your handle error method, that you are using in your script. // if you prefer you can comment this line, as it will return false, than you handle the error, from the place you call this function. echo'<b style="color: red">The delimiter '.$delimeter.' was not found in the string '.$string.'</b>'; // return false because the delimeter was not found in the $string, so that you can decide what you want to do. return false; } // will return one arry with the $limit you have set or the default one that is 100 fields in the array. return $explode_array; } /** * This function will detect all empty fields in the array. * @param string $var - will have the value contained in the array field. * @return bolean - true if it not find a empty value or false if find the value empty. * * @author kkobashi */ function explode_filtered_empty($var) { if ($var == "") return(false); return(true); } ?> Disclaimer: Use at your own risk. In my script works as expected.

« previous php.notes (#190670) next »