如何匹配IP地址与逗号分隔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

This is what i m sharing with you guys. have spent almost 2 to 3 hours for this so i would like to share this with you.so you may not have to spent hours for this.

this is the regular expression for match the IP with comma separate string.

/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})*$/

Link for check online regular expression

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