Voting

: max(eight, five)?
(Example: nine)

The Note You're Voting On

wjaspers4 [at] gmail [dot] com
16 years ago
I recently encountered a problem trying to capture multiple instances of named subpatterns from filenames.
Therefore, I came up with this function.

The function allows you to pass through flags (in this version it applies to all expressions tested), and generates an array of search results.

Enjoy!

<?php

/**
* Allows multiple expressions to be tested on one string.
* This will return a boolean, however you may want to alter this.
*
* @author William Jaspers, IV <[email protected]>
* @created 2009-02-27 17:00:00 +6:00:00 GMT
* @access public
*
* @param array $patterns An array of expressions to be tested.
* @param String $subject The data to test.
* @param array $findings Optional argument to store our results.
* @param mixed $flags Pass-thru argument to allow normal flags to apply to all tested expressions.
* @param array $errors A storage bin for errors
*
* @returns bool Whether or not errors occurred.
*/
function preg_match_multiple(
array
$patterns=array(),
$subject=null,
&
$findings=array(),
$flags=false,
&
$errors=array()
) {
foreach(
$patterns as $name => $pattern )
{
if(
1 <= preg_match_all( $pattern, $subject, $found, $flags ) )
{
$findings[$name] = $found;
} else
{
if(
PREG_NO_ERROR !== ( $code = preg_last_error() ))
{
$errors[$name] = $code;
} else
$findings[$name] = array();
}
}
return (
0===sizeof($errors));
}
?>

<< Back to user notes page

To Top