note 67949 deleted from function.preg-match-all by colder

From: Date: Mon, 16 Apr 2007 17:04:04 +0000
Subject: note 67949 deleted from function.preg-match-all by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125472@lists.php.net to get a copy of this message
Note Submitter: volkank at developera dot com ---- 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.

« previous php.notes (#125472) next »