phpvalidation

How can I simply validate whether a string is a valid IP in PHP?


Using PHP, how do I validate that a string is a valid IP?

Examples of valid strings:

Examples of invalid strings:

My current script uses this code, but this is insufficient for my needs:

if(strpos($input, '.') !== false)
{    
  // There is a period 
} 
else 
{     
  // No Period 
} 

As such, can someone please advise how I can validate that a string is a valid IP?


Solution

  • $valid = ip2long($ip) !== false;