Ip address validation in PHP using regular expression

If you don’t know how to validate the IP address format in PHP, then you are in the right place.I’ll show you here how to validate the IP address using regular expression in PHP.

As you guyz know, IP address consists four parts. Each parts separated by period “.” and these part consists the digits which ranges from 0 to 255.

Function to validate IP address in PHP using Regular Expression

//function to validate ip address format in php by Roshan Bhattarai(http://roshanbh.com.np)
function validateIpAddress($ip_addr)
{
  //first of all the format of the ip address is matched
  if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
  {
    //now all the intger values are separated
    $parts=explode(".",$ip_addr);
    //now we need to check each part can range from 0-255
    foreach($parts as $ip_parts)
    {
      if(intval($ip_parts)>255 || intval($ip_parts)<0)
      return false; //if number is not within range of 0-255
    }
    return true;
  }
  else
    return false; //if format of ip address doesn't matches
}

As you can see above, first of all the format of the “$ip_addr” is validated using regular expression. In the regular expression “\d{1,3}” means that there should be digits which can be either 1 to 3 digits because a IP Adress can be “222.0.123.12” or
“12.15.123.5”. So, each part can consists 1 to 3 digits.

After validating the format using regular expression, each part of the IP address is separated using period(“.”) using explode() function available in PHP. And finally, it is checked that each part of the IP address is between 0 to 225 or not.

12 thoughts on “Ip address validation in PHP using regular expression

  1. CC

    can you not simply use a regexp of: –

    ^[0-255]\.[0-255]\.[0-255]\.[0-255]$

    i.e. four integers in the range of 0 to 255 with a period separating them?

  2. It won’t work…try it…it will just validate a number like 0,1,2 or 5 only since it is equivalent to character set [0125]

  3. Brian Szymanski

    Or you could do the whole kit+kaboodle in one regexp like so:

    return preg_match(“/^((\d{1,2}|[01]\d{2}|2([0-4]\d|5[0-5]))\.){3}(\d{1,2}|[01]\d{2}|2([0-4]\d|5[0-5]))$/”,$ip_addr))

  4. Decon

    You might find this URL useful (and faster without using reg-ex)

    http://www.jimgrill.com/pages/content/validateip

  5. Hi Decon…thanks for sharing that link…

  6. Juanra

    Another way to do this:

    function validateIpAddress($ip_addr) {
    $long = ip2long($ip_addr);
    return !($long == -1 || $long === FALSE);
    }

  7. john

    If you are checking for a valid IP address the first octet should be less than 224 – above this are reserved for multicast addresses. You may also want to check for broadcast addresses but this woupld require a lot more testing based on the Class of IP address. You may also want to check for RFC 1918 private addresses and well as 127.x.x.x looopback. I do not think regular expressions are they way to do this and it will require a proper php function. Here is my basic function just checking for a valid unicast IP address….

    function check_ip_address($ip_address) {

    $octets = explode(‘.’,$ip_address);

    if (($octets[0] > 0 && $octets[0] 0 && $octets[1] 0 && $octets[2] 0 && $octets[3] < 255) &&
    (count($octets) == 4))

    { return TRUE; }

    }

  8. john

    Sorry don’t know why but the code is not pasting correctly try again…..//

    function check_ip_address($ip_address) {
    $octets = explode(‘.’,$ip_address);
    if (($octets[0] > 0 && $octets[0] 0 && $octets[1] 0 && $octets[2] 0 && $octets[3] < 255) && (count($octets) == 4))
    { return TRUE; }
    }

  9. Tim

    All of these are rather complex and unless you want a very specific error message why not just use…

    function checkIP($ip)
    {
    $cIP = ip2long($ip);
    $fIP = long2ip($cIP);
    return $fIP;
    }

    It will return 0.0.0.0 if an invalid IP or string is passed to it.

  10. Thanks for the code sample. It helps me to understand the preg_match command a little bit more.

  11. Thanks for this code. It is very useful.

  12. shruti joshi

    Thank you for giving code of IP validation.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>