note 71000 deleted from function.eregi by cmb
| From: | cmb@php.net | Date: | Sun, 20 Oct 2019 08:43:42 +0000 |
| Subject: | note 71000 deleted from function.eregi by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212680@lists.php.net to get a copy of this message | ||
Note Submitter: m at tthew dot org dot uk
----
This example checks for a valid IP address or CIDR notation address range. (Thanks Walo for just the
start I needed.)
The reg exp is too long to post in the code. It is:
^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}$
(substitute for EXPR)
<?php
function checkValidIp($cidr) {
// Checks for a valid IP address or optionally a cidr notation range
// e.g. 1.2.3.4 or 1.2.3.0/24
if(!eregi("EXPR", $cidr)) {
$return = FALSE;
} else {
$return = TRUE;
}
if ( $return == TRUE ) {
$parts = explode("/", $cidr);
$ip = $parts[0];
$netmask = $parts[1];
$octets = explode(".", $ip);
foreach ( $octets AS $octet ) {
if ( $octet > 255 ) {
$return = FALSE;
}
}
if ( ( $netmask != "" ) && ( $netmask > 32 ) ) {
$return = FALSE;
}
}
return $return;
}
?>