Search code examples
phppreg-match

How to match Ip address with comma separated Ips php


How to match valid ip with (,) separated string.

I have one text box and user can input ip with comma (,) separated.

for that i m checking that the string contain valid ip string.

String should be

192.168.1.1
195.138.124.1,1.154.127.1

Solution

  • As suggested, you will obtain stronger results using build-in filters:

    function validateIPs ($input) {
        foreach(explode(',', $input) as $ip)
            if (!filter_var($ip, FILTER_VALIDATE_IP))
                return false;
        return true;
    }