note 30306 modified in function.explode by aidan
| From: | aidan@php.net | Date: | Sun, 27 Jun 2004 14:27:34 +0000 |
| Subject: | note 30306 modified in function.explode by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71926@lists.php.net to get a copy of this message | ||
Here's a function I find useful from time to time.... When you're assigning the results of
an explode() to a list(x,y,z) of values, you'll get an error/warning in PHP if your explode()
didn't return the same number of arguments as your list... So you can use the following simple
function to always return the correct number of elements in your array:
<?php
// explodeForce - like explode, but rather than just 'limit' the explosion to
// the number of values, it also ensures that this number of elements are
// present in the output array - v.useful for when assigning an explode
// to a list($a,$b,$c)
function explodeForce($sep, $array, $number_values, $pad = '')
{
return array_pad(explode($sep, $array, $number_values), $number_values, $pad);
}
?>
--was--
Here's a function I find useful from time to time.... When you're assigning the results of
an explode() to a list(x,y,z) of values, you'll get an error/warning in PHP if your explode()
didn't return the same number of arguments as your list... So you can use the following simple
function to always return the correct number of elements in your array:
// explodeForce - like explode, but rather than just 'limit' the explosion to
// the number of values, it also ensures that this number of elements are
// present in the output array - v.useful for when assigning an explode
// to a list($a,$b,$c)
function explodeForce($sep, $array, $number_values, $pad = '')
{
return array_pad(explode($sep, $array, $number_values), $number_values, $pad);
}
http://php.net/manual/en/function.explode.php