note 39792 deleted from function.preg-match by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 18:16:03 +0000 |
| Subject: | note 39792 deleted from function.preg-match by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127580@lists.php.net to get a copy of this message | ||
Note Submitter: selt
----
Concerning a list of notes started on November 11; ie
<?
$num="([0-9]|1?\d\d|2[0-4]\d|25[0-5])";
?>
It is interesting to note that the pattern matching is done using precedence from left to right,
therefore; an address such as 127.0.0.127 sent to preg_match with a hash for the matched patterns
would return 127.0.0.1.
so, to obtain a proper mechanism for stripping valid IPs from a string (any string that is) one
would have to use:
<?
function valid_ipv4($ip_addr)
{
$num="(1?\d\d|2[0-4]\d|25[0-5]|[0-9])";
$range="([1-9]|1\d|2\d|3[0-2])";
if(preg_match("/$num\.$num\.$num\.$num/",$ip_addr,$matches))
{
return $matches[0];
} else {
return false;
}
}
?>
thanks for all the postings ! They're the best way to learn.