Ip address validation in PHP using regular expression

Advertisement

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.

Popularity: 7% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Email address validation in PHP
» Getting real IP address in PHP
» W3c markup validation and Big websites – Is it really needed?
» Getting country , city name from IP address in PHP

12 Comments on “Ip address validation in PHP using regular expression”

  • CC wrote on 25 April, 2008, 13:51

    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?

  • Roshan wrote on 25 April, 2008, 15:47

    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]

  • Brian Szymanski wrote on 25 June, 2008, 17:27

    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))

  • Decon wrote on 8 August, 2008, 13:18

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

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

  • Roshan wrote on 8 August, 2008, 15:57

    Hi Decon…thanks for sharing that link…

  • Juanra wrote on 19 August, 2009, 17:44

    Another way to do this:

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

  • john wrote on 18 December, 2009, 11:13

    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; }

    }

  • john wrote on 18 December, 2009, 11:17

    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; }
    }

  • Tim wrote on 18 February, 2010, 10:32

    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.

  • Michael Pedzotti wrote on 29 May, 2010, 15:20

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

  • Mihai Voiculescu wrote on 6 February, 2011, 14:14

    Thanks for this code. It is very useful.

  • shruti joshi wrote on 16 April, 2012, 2:12

    Thank you for giving code of IP validation.

Write a Comment

 


Copyright © 2014 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.