note 70475 added to function.preg-match-all
| From: | php-general at lists dot php dot net | Date: | Wed, 18 Oct 2006 12:09:46 +0000 |
| Subject: | note 70475 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118683@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 129.16.214.50
Probable Submitter: 81.230.98.72
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- https://master.php.net/note/edit/70475
Del: integrated -- https://master.php.net/note/delete/70475/integrated
Del: useless -- https://master.php.net/note/delete/70475/useless
Del: bad code -- https://master.php.net/note/delete/70475/bad+code
Del: spam -- https://master.php.net/note/delete/70475/spam
Del: non-english -- https://master.php.net/note/delete/70475/non-english
Del: in docs -- https://master.php.net/note/delete/70475/in+docs
Del: other reasons-- https://master.php.net/note/delete/70475
Reject -- https://master.php.net/note/reject/70475
Search -- https://master.php.net/manage/user-notes.php