note 101201 deleted from function.preg-match by danbrown
| From: | danbrown@php.net | Date: | Thu, 02 Dec 2010 06:38:35 +0000 |
| Subject: | note 101201 deleted from function.preg-match by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174547@lists.php.net to get a copy of this message | ||
Note Submitter: blasterjr at gmail dot com
----
In regards to the previous poster:
<?php
$str ='/^00+[0-1]+[0-1]+[0-1]
+[0-1]+[0-1]+[0-1]+00+[0-1]+
[0-1]+[0-1]+[0-1]+[0-1]+[0-1]+00+
[0-1]+[0-1]+[0-1]+[0-1]+[0-1]+[0-1]+00/i';
if ((preg_match($str,"00000000000000000000000000")))
{echo "OK";}
?>
Not work
thats because your statement is doing the following:
contains 00, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
contains 00, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
contains 00, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
0 or 1, one or more times
contains 00, one or more times
So using your string of: "00000000000000000000000000"
matches your VERY first conditional of "00+" which states
one or more occurrances, so that will account for every set of 2 0's if you have a 1, it would
then look at the [0-1]+ because it no longer matches the 00+ statement, and it would encompass the
rest of your string from that point on, to check against such things,
this would be the proper regex statement/syntax:
<?php
$pattern = "/^(00[01]{6}00[01]{6}00[01]{6}00)$/"
$subject = array(
'00000000000000000000000000', /* Works */
'00000000000000000000000100', /* Works */
'00111111001111110011111100', /* Works */
'00000000000000000000001000', /* Works */
'00000000000000000000001001', /* Doesn't Work */
'10000000000000000000000000', /* Doesn't Work */
'11111111111111111111111111', /* Doesn't Work */
);
$status = preg_match($pattern,$subject,$matches);
if($status === false){
echo "An error occurred";
} else if($status == 0){
echo "No Results Found";
} else if($status > 0){
echo "OK";
}
?>
The code I've provided is a reworked version of your purposing, and will work flawlessly as
provided using your string structure.
a simple description of how mine works:
require the string to begin with two 0's, then, followed by either a 0 or a 1, 6 times, then
two 0's again, followed once again by either a 0 or a 1, 6 times, followed by another pair of
0's, with 0 or 1 6 times following that, end finally ending in a pair of 0's.
the i, in this case, was not needed, as it represents case insensitive searching, but we're
only working with integers here, so we do not need such things, you maybe want to add m to the end
to support multiline, and use preg_match_all to get every one of the records it finds.
I hope this can help someone else understand the principals behind formulating a regex statement.