|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 :)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 05:00:02 2025 UTC |
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. DerickI 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