note 76653 deleted from function.preg-match by felipe

From: Date: Sun, 23 Mar 2008 22:55:07 +0000
Subject: note 76653 deleted from function.preg-match by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137371@lists.php.net to get a copy of this message
Note Submitter: alexandre at NO-DAMN-SPAM-BOTS-gaigalas dot net ---- Match and replace for arrays. Useful for parsing entire $_POST Only array_preg_match examples: <?php function array_preg_match(array $patterns, array $subjects, &$errors = array()) { $errors = array(); foreach ($patterns as $k => $v) preg_match($v, $subjects[$k]) or $errors[$k] = TRUE; return count($errors) == 0 ? TRUE : FALSE; } function array_preg_replace(array $patterns, array $replacements, array $subject) { $r = array(); foreach ($patterns as $k => $v) $r[$k] = preg_replace($v, $replacements[$k], $subject[$k]); return $r+$subject; } $arr1 = array('name' => 'Alexandre', 'phone' => '44559999'); $arr2 = array('name' => '', 'phone' => '44559999c'); array_preg_match(array( 'name' => '#.+#', //Not empty 'phone' => '#^$|(\d[^\D])+#' // Only digits, optional ), $arr1, $match_errors); print_r($match_errors); // Empty, it is ok. array_preg_match(array( 'name' => '#.+#', //Not empty 'phone' => '#^$|(\d[^\D])+#' // Only digits, optional ), $arr2, $match_errors); print_r($match_errors); // Two indexes, name and phone, both not ok. ?>

« previous php.notes (#137371) next »