note 109978 added to function.explode
| From: | exadra37 at nospam dot asualojaonline dot com | Date: | Fri, 07 Sep 2012 13:02:40 +0000 |
| Subject: | note 109978 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190568@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 194.8.30.16
Probable Submitter: 81.84.194.12
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/109978
Del: integrated -- https://master.php.net/note/delete/109978/integrated
Del: useless -- https://master.php.net/note/delete/109978/useless
Del: bad code -- https://master.php.net/note/delete/109978/bad+code
Del: spam -- https://master.php.net/note/delete/109978/spam
Del: non-english -- https://master.php.net/note/delete/109978/non-english
Del: in docs -- https://master.php.net/note/delete/109978/in+docs
Del: other reasons-- https://master.php.net/note/delete/109978
Reject -- https://master.php.net/note/reject/109978
Search -- https://master.php.net/manage/user-notes.php