#21277 [WFx->Fbk]: Suggestion: New function, is_ip

From: Date: Mon, 30 Dec 2002 08:15:30 +0000
Subject: #21277 [WFx->Fbk]: Suggestion: New function, is_ip
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-28729@lists.php.net to get a copy of this message
ID: 21277 Updated by: jmcastagnetto@php.net Reported By: webmaster@emugenerations.com -Status: Won't fix +Status: Feedback Bug Type: Feature/Change Request Operating System: Linux PHP Version: 4.2.3 New Comment: The code cannot only be implemented in one regex, but the ones show above, using substr_count() or preg_match, are buggy as they support addresses of the form 999.999.999.999, which is clearly incorrect. A better regex will have to take into account that IP v4 addresses range from 0.0.0.0 to 255.255.255.255 Don't see a real driving need to implement as part of the language. Previous Comments: ------------------------------------------------------------------------ [2002-12-30 00:32:19] derick@php.net Can be done with a oneliner: (something along the lines of:) if (preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$/", $ip) { /* It is an IP */ } Not really worth the trouble to implement it as a PHP code feature. Derick ------------------------------------------------------------------------ [2002-12-29 18:54:21] webmaster@emugenerations.com function is_ip($str) // Returns True if the given string is a IP address { if ((substr_count($str, ".") != 3) or (empty($str))) { return false; } $ip_array = explode(".", $str); for ($i = 0; $i < count($ip_array); $i++) { if ((strlen($ip_array[$i]) != 3) or (!is_numeric($ip_array[$i]))) { return false; } } return true; } I think that this function could be useful for many PHP coders :) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=21277&edit=1

« previous php.bugs (#28729) next »