note 31192 added to function.ereg

From: Date: Sat, 12 Apr 2003 20:24:56 +0000
Subject: note 31192 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-46551@lists.php.net to get a copy of this message
hey, I needed a simple check to see if a string is some kind of IP Address.. (IPv6, IPv4, LongIP) Here is an example: if (ereg("^([0-9]{1,3}(\.[0-9]{1,3}){3})$| ^([0-9]{8,10})$| ^([0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}) {2,7}(([0-9]{1,3}(\.[0-9]{1,3}){3})?| (/[0-9]+)?))$", "127.0.0.1")) { echo "correct"; } (line wrapped) This isn't perfect at all, for example, ips such as 324.552.112.33 will be accepeted too. (same for longips) here are the regex broken apart: Ip4v detection: ^([0-9]{1,3}(\.[0-9]{1,3}){3})$ longip detection: ^([0-9]{8,10})$ ipv6 detection: ^([0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}){2,7} (([0-9]{1,3}(\.[0-9]{1,3}){3})?|(/[0-9]+)?))$ If you have a better way please tell me. Thanks. -- http://www.php.net/manual/en/function.ereg.php http://master.php.net/manage/user-notes.php?action=edit+31192 http://master.php.net/manage/user-notes.php?action=delete+31192 http://master.php.net/manage/user-notes.php?action=reject+31192

« previous php.notes (#46551) next »