php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #21277 Suggestion: New function, is_ip
Submitted: 2002-12-29 18:54 UTC Modified: 2002-12-30 02:20 UTC
Votes:7
Avg. Score:3.1 ± 1.1
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: webmaster at emugenerations dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.2.3 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: webmaster at emugenerations dot com
New email:
PHP Version: OS:

 

 [2002-12-29 18:54 UTC] webmaster at emugenerations dot 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 :)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-30 00:32 UTC] 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-30 02:15 UTC] jmcastagnetto@php.net
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.
 [2002-12-30 02:20 UTC] derick@php.net
I said "along the lines of"... meaning "as an example". ANd yes, it's   
perfectly possible to do this in one regexp:

/1?[0-9]{0,2}|2[0-4][0-9]|25[0-5]\.1?[0-9]{0,2}|2[0-4][0-9]|25[0-5]\.1?[0-9]{0,2}|2[0-4][0-9]|25[0-5]\.1?[0-9]{0,2}|2[0-4][0-9]|25[0-5]/

though I agree this is not really nice. As we both think that it is not worth a new function, i'm setting it to "Won't fix" again.

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 12:01:32 2024 UTC