note 76653 added to function.preg-match
| From: | alexandre at NO-DAMN-SPAM-BOTS-gaigalas dot net | Date: | Tue, 24 Jul 2007 20:44:22 +0000 |
| Subject: | note 76653 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128893@lists.php.net to get a copy of this message | ||
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.
?>
----
Server IP: 200.185.109.13
Probable Submitter: 201.74.16.132
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- https://master.php.net/note/edit/76653
Del: integrated -- https://master.php.net/note/delete/76653/integrated
Del: useless -- https://master.php.net/note/delete/76653/useless
Del: bad code -- https://master.php.net/note/delete/76653/bad+code
Del: spam -- https://master.php.net/note/delete/76653/spam
Del: non-english -- https://master.php.net/note/delete/76653/non-english
Del: in docs -- https://master.php.net/note/delete/76653/in+docs
Del: other reasons-- https://master.php.net/note/delete/76653
Reject -- https://master.php.net/note/reject/76653
Search -- https://master.php.net/manage/user-notes.php