// test string
$string = '192.168.1.1,10.0.0.1,1.2.3.4,BAD:256.257.258.259';
// first must be between 1-255
$npf = '([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])';
// next must be between 0-255
$npn = '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])';
// ip has to be bordered by \b regex to be valid
$pattern = "~\\b{$npf}\\.{$npn}\\.{$npn}\\.{$npn}\\b~s";
// replace with a callback
$string = preg_replace_callback($pattern, function($match){
// play with them as you wish
$ip = $match[0];
$match1 = intval($match[1]);
if(in_array($match1, array(10, 192))){
return $ip;
}
return '<ip>';
}, $string);
// clean up
unset($npf, $npn);
// output string
var_dump($string);
Code is commented, should make sense.
BONUS:
These are the private IP ranges.
$private_ranges = array(
// 10.0.0.1 to 10.255.255.254
ip2long('10.0.0.0') => ip2long('10.255.255.255'),
// 172.16.0.1 to 172.31.255.254
ip2long('172.16.0.0') => ip2long('172.31.255.255'),
// 192.168.0.1 to 192.168.255.254
ip2long('192.168.0.0') => ip2long('192.168.255.255'),
// 127.0.0.0 to 127.255.255.255
ip2long('127.0.0.0') => ip2long('127.255.255.255'),
);
Use them in an array with ip2long()
against $match[0]
for better hit testing.