简体   繁体   中英

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 im checking that the string contain valid ip string.

String should be

192.168.1.1
195.138.124.1,1.154.127.1

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

This is what im 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM