note 70475 deleted from function.preg-match-all by colder
| From: | colder@php.net | Date: | Mon, 16 Apr 2007 16:52:38 +0000 |
| Subject: | note 70475 deleted from function.preg-match-all by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125451@lists.php.net to get a copy of this message | ||
Note Submitter:
----
While reading these notes I noticed many IP-matching patterns that seemed to be missing a detail.
Most use this pattern...
25[1-5] | 2[1-4]\d | [01]\d{2} | \\d{1,2}
... for matching each digit group to make sure not to match anything above 255. If we have something
looking like an IP starting with 260 it can however match the 60.
So, in the beginning, if matching less than 3 digits, make sure there is no digit before:
(?<=[^\d]|^)
And at the end, make sure there is no digit following: (?=[^\d]|$)
<?php
$ip_pattern = "/((25[1-5]|2[1-4]\\d|[01]\\d{2}|(?<=[^\\d]|^)\\d{1,2})\\.){3}".
"(25[1-5]|2[1-4]\\d|[01]\\d{2}|\\d{1,2})(?=[^\\d]|$)/";
?>
But generally it takes less time (coding and executing) to just capture anything looking like an IP
and weed out the invalid ones with some simple function using array_walk(). A lot more flexible as
well.