note 67948 added to function.preg-match

From: Date: Fri, 07 Jul 2006 14:01:35 +0000
Subject: note 67948 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-114546@lists.php.net to get a copy of this message
I will add some note about my last post. Leading zeros in IP addresses can cause problems on both Windows and Linux, because one can be confused if it is decimal or octal (if octal not written properly) "66.163.161.117" is in a decimal syntax but in "066.163.161.117" the first octet 066 is in octal syntax. So "066.163.161.117" is recognized as decimal "54.163.161.117" by the operating system. BTW octal is alittle rare syntax so you may not want or need to match it. *** Unless you specially want to match IP addresses including both decimal and octal syntax; you can use Chortos-2's pattern which is suitable for most conditions. <?php //DECIMAL syntax IP match //$num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])"; $num='(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])'; if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $ip_addr,$match)) //validate IP ... preg_match_all("/$num\\.$num\\.$num\\.$num/",$test,$match); //collect IP addresses from a text(notice that ^$ not present in pattern) ... ?> *** Also my previous pattern still have bug and needs some changes to correctly match both decimal and octal syntax. ---- Server IP: 66.163.161.117 Probable Submitter: 81.213.95.92 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/note/edit/67948 Del: integrated -- http://master.php.net/note/delete/67948/integrated Del: useless -- http://master.php.net/note/delete/67948/useless Del: bad code -- http://master.php.net/note/delete/67948/bad+code Del: spam -- http://master.php.net/note/delete/67948/spam Del: non-english -- http://master.php.net/note/delete/67948/non-english Del: in docs -- http://master.php.net/note/delete/67948/in+docs Del: other reasons-- http://master.php.net/note/delete/67948 Reject -- http://master.php.net/note/reject/67948 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114546) next »