Once a geek, forever a geek…

Verify IPv4 address in php using regex

| 2 Comments

A simple function in php to verify IPV4 Address. It is completely based in regex and does full ip verifying.
It tests the ip for;
1. Need 4 numeric blocks separated by a dot.
2. Each numeric block must noot exceed 255.
3. Shouldn’t contain space. So remember to trim before calling this function.

function isINetAddress($ipaddr){
if( preg_match( "/^((?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9]).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/m",$ipaddr) > 0)
return true;
}

This can’t me more simpler. ;)
Enjoy!

Related Posts

Author: Marius Voila

Hi! My name is Marius Voila, and I am a professional system administrator, system architect, and designer.

2 Comments

  1. function isINetAddress($ipaddr)
    {
    return (boolean)preg_match( “/^((?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9]).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/m”,$ipaddr);
    }

    de ce nu a?a? plus dac? nu coincide returneaz? false în loc de null..

  2. Merge ?i a?a :)

Leave a Reply

Required fields are marked *.

*