str_multicut, découper une chaine de caractères
By Geoffrey on Wednesday 7 June 2006, 10:38 - Coding - Permalink
/** * Cuts a string according to defined breakpoints * * @param string $string The string to cut * @param int $breakpoint,... Breakpoints * @return array * * - Cut the given string according to given breakpoints * and return a array with each part. If the last breakpoint * does not go to the end of the string, the remaining * string part is appended to the result array. * - str_multicut will stop cutting on (whichever comes first): * end of string, no more breakpoints * - If there is no breakpoint, str_multicut will return * an empty array * * - example: str_multicut('foobar', 3, 2) will return: * array('foo', 'ba', 'r'); */ function str_multicut($string) { $args = func_get_args(); $string = array_shift($args); $result = array(); while (strlen($string) > 0 && !empty($args)) { $breakpoint = array_shift($args); $result[] = substr($string, 0, $breakpoint); $string = substr($string, $breakpoint); } return $result; }
Comments
Amusant :)
c'est aussi pas mal utile pour parser les dates mal formatée genre "20060607", un coup de str_multicut($date, 4, 2, 2) et on en parle plus
Deux en un : (très) utile et 1337.