note 89252 added to function.preg-match

From: Date: Fri, 27 Feb 2009 23:16:08 +0000
Subject: note 89252 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-151239@lists.php.net to get a copy of this message
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 <wjaspers4@gmail.com> * @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)); } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 209.150.201.146 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/89252 Del: integrated -- https://master.php.net/note/delete/89252/integrated Del: useless -- https://master.php.net/note/delete/89252/useless Del: bad code -- https://master.php.net/note/delete/89252/bad+code Del: spam -- https://master.php.net/note/delete/89252/spam Del: non-english -- https://master.php.net/note/delete/89252/non-english Del: in docs -- https://master.php.net/note/delete/89252/in+docs Del: other reasons-- https://master.php.net/note/delete/89252 Reject -- https://master.php.net/note/reject/89252 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151239) next »