note 39792 added to function.preg-match
| From: | selt at rn2 dot php dot net | Date: | Tue, 10 Feb 2004 20:11:04 +0000 |
| Subject: | note 39792 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64979@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39792
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39792&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39792&report=yes
Search -- http://master.php.net/manage/user-notes.php