如何使用php提取所有IP地址

时间:2014-07-08 17:31:50

标签: php iptables

请如何从给定字符串中提取所有IP地址。

例如:

"
2014-07-08 19:05:20 1X4YpU-0001kr-6y <= info@arianet-dsl.com H=(server.takcloud.com) [185.4.28.203] P=esmtps X=TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256 S=1018 id=57844562-138B-4934-9CF7-554F8C613C1C@arianet-dsl.com
"

1 个答案:

答案 0 :(得分:3)

我会使用RegEx

搜索由“。”分隔的最多三个“{1,3}”数字“\ d”的组。

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

在PHP中使用它

$mailStr = "2014-07-08 19:05:20 1X4YpU-0001kr-6y <= info@arianet-dsl.com H=(server.takcloud.com) [185.4.28.203] P=esmtps X=TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256 S=1018 id=57844562-138B-4934-9CF7-554F8C613C1C@arianet-dsl.com";
$regexpattern = "/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/";
preg_match_all($regexpattern , $mailStr, $matches);
print_r($matches);