【问题标题】:How to extract all IP address using php [duplicate]如何使用php提取所有IP地址[重复]
【发布时间】:2014-08-29 13:27:25
【问题描述】:

请问如何从给定的字符串中提取所有 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
"

【问题讨论】:

    标签: php iptables


    【解决方案1】:

    我会使用正则表达式

    搜索由“.”分隔的最多三个“{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);
    

    【讨论】:

    • +1 您在示例代码中错过了分隔符。 preg_match_all() 会找到所有的 IP 地址。
    • 你想给我看一个带有正确分隔符的例子吗?
    猜你喜欢
    • 2012-11-03
    • 2014-10-21
    • 2023-04-08
    • 2013-03-19
    • 2011-04-08
    • 2011-07-12
    • 2018-08-18
    • 2011-09-09
    • 2016-04-09
    相关资源
    最近更新 更多