除了日期

时间:2017-11-10 09:49:40

标签: php regex

我目前正在PHP模板上输出一些Whois信息(使用this),它通过此变量$whois_answer返回大量文本:

  

相关日期:注册时间:2015年8月19日到期日:2025年8月19日   最后更新时间:2015年9月21日注册状态:注册到期   日期。名称服务器:xxx.mainnameserver.com xxx.mainnameserver.com   WHOIS查询于2017年10月10日09:34:40进行 - 此WHOIS信息为   由Nominet UK免费提供.uk域名的中央注册表   名。此信息和.uk WHOIS是:版权所有Nominet UK   1996年至2017年。您不得访问.uk WHOIS或使用其中的任何数据   除非完全可用的使用条款允许   http://www.nominet.uk/whoisterms,其中包括以下限制:(A)   将数据用于广告或重新包装,重新编译,   重新分配或重用(B)模糊,删除或隐藏任何或所有   本通知和(C)超出查询率或数量限制。数据   是按“原样”提供的,可能会落后于登记册。   权限可随时被取消或限制。域是   注册

我不想修改原始代码,只修改最终输出(因为我可能会在以后使用其他功能)。

我纯粹想要获取域名的到期日期,我该如何定位并删除其他所有内容?请注意,域名是动态的,因此日期可以是任何内容,但始终采用该格式。

1 个答案:

答案 0 :(得分:1)

使用preg_match:

$str ="Relevant dates: Registered on: 19-Aug-2015 Expiry date: 19-Aug-2025 Last updated: 21-Sep-2015 Registration status: Registered until expiry date. Name servers: xxx.mainnameserver.com xxx.mainnameserver.com WHOIS lookup made at 09:34:40 10-Nov-2017 -- This WHOIS information is provided for free by Nominet UK the central registry for .uk domain names. This information and the .uk WHOIS are: Copyright Nominet UK 1996 - 2017. You may not access the .uk WHOIS or use any data from it except as permitted by the terms of use available in full at http://www.nominet.uk/whoisterms, which includes restrictions on: (A) use of the data for advertising, or its repackaging, recompilation, redistribution or reuse (B) obscuring, removing or hiding any or all of this notice and (C) exceeding query rate or volume limits. The data is provided on an 'as-is' basis and may lag behind the register. Access may be withdrawn or restricted at any time. Domain is registered";

Preg_match("/Expiry date:\s([a-zA-Z0-9-]+)/", $str, $match);

Echo $match[1];

https://3v4l.org/k7W2b

这将匹配"到期日期"然后捕获:a-Z0-9以及-意味着它将停止在太空捕获。