使用PHP从字符串中提取不同格式的日期

时间:2012-11-23 17:43:17

标签: php preg-match extract

我正在使用 cURL和WHOIS查询来检索不同网站的注册信息。

问题是WHOIS查询返回的字符串包含不同格式的日期。

我想使用preg_match 将创建日期和到期日期提取到单独的数组中,如:

$creation = array(year, month, day);
$expiration = array(year, month, day);

不同的字符串输出格式为:

    expires: 2014-01-10 14:00:27
    created: 2012-01-10 15:00:28

    Creation Date: 25-Mar-2010  
    Expiration Date: 25-Mar-2013

    Record created:     14/04/2012
    Record expires:     14/04/2013

    Record created on.......: Mar 05, 2001  
    Record expires on.......: Mar 05, 2013

    Created on..............: 1997-09-15.
    Expires on..............: 2020-09-13.

    Expiration Date: 2015-12-26 19:18:07
    Creation Date:   2003-12-26 19:18:07

有关如何使用preg_match提取日期的任何线索?

注意:我尝试过使用PHP Whois API类但它运行不正常,并且不同的XML API但数据不完整。

1 个答案:

答案 0 :(得分:0)

我实际上已经完成了这个功能:

function creationdate($data)
{

if(preg_match('/Creation Date:/i',$data) == true){
    $csta = strpos($data, 'Creation Date:', 0)+14;
    $cstb = strpos($data, 'Creation Date:', 0)+27;
    $date = trim(substr($data, $csta, $cstb-$csta));
return strtotime($date);};

if(preg_match('/Record created:/i',$data) == true){
    $csta = strpos($data, 'Record created:', 0)+15;
    $cstb = strpos($data, 'Record expires:', $csta)-2;
    $date = substr($data, $csta, $cstb-$csta);
    $date = explode('/', $date);
    $date = $date[0] .'-'. $date[1] .'-'. $date[2];
return strtotime($date);};

if(preg_match('/Created on..............:(.*?)\./i',$data,$date) == true){
return strtotime($date[1]);};

if(preg_match('/Record created on.......:/i',$data) == true){
    $csta = strpos($data, 'Record created on.......:', 0)+26;
    $cstb = strpos($data, 'Record expires on.......:', $csta)-5;
    $date = substr($data, $csta, $cstb-$csta);
return strtotime($date);};

if(preg_match('/created:/i',$data) == true){
    $csta = strpos($data, 'created:', 0)+9;
    $cstb = strpos($data, 'created:', 0)+20;
    $date = trim(substr($data, $csta, $cstb-$csta));
return strtotime($date);};

return '-';

}