note 25948 modified in function.array-search by vrana

From: Date: Tue, 17 Aug 2004 14:49:13 +0000
Subject: note 25948 modified in function.array-search by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79992@lists.php.net to get a copy of this message
If you'd rather search for values in an array that match a regular expression, this function will do the trick (note that it returns the first index matched, not the last key matched as array_search does): <?php /** * Searches for a regex match in every value of a one-dimensional array. * * @param string $pattern POSIX-extended regular expression to match. * @param array $haystack Array to search in. * @return mixed Index of the first match, or FALSE if no match is found. */ function array_ereg($pattern, $haystack) { for($i = 0; $i < count($haystack); $i++) { if (ereg($pattern, $haystack[$i])) return $i } return false; } ?> --was-- If you'd rather search for values in an array that match a regular expression, this function will do the trick (note that it returns the first index matched, not the last key matched as array_search does): /** * Searches for a regex match in every value of a one-dimensional array. * * @param string $pattern POSIX-extended regular expression to match. * @param array $haystack Array to search in. * @return mixed Index of the first match, or FALSE if no match is found. */ function array_ereg($pattern, $haystack) { for($i = 0; $i < count($haystack); $i++) { if (ereg($pattern, $haystack[$i])) return $i } return false; } http://php.net/manual/en/function.array-search.php

« previous php.notes (#79992) next »