【问题标题】:Regex to grab Date from String正则表达式从字符串中获取日期
【发布时间】:2012-02-15 22:12:10
【问题描述】:

我正在尝试获取代表日期的字符串的一部分。

日期字符串通常(但不总是)在它之前和/或之后有常规文本。

在这个例子中:

Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here

我希望结果是:

Sun, Apr 09, 2000

请记住,日期和月份字符串的长度可以是 3 或 4 个字符。

我微不足道的尝试是:

$test = "Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here";

if (ereg ("/([a-z]{3,4}),.([a-z]{3,4}).([0-9]{1,2}),.([0-9]{4})/i", $test, $regs)) {
    echo "$regs[4].$regs[3].$regs[2].$regs[1]";
}

也有兴趣了解基于非正则表达式的解决方案。

【问题讨论】:

    标签: php regex parsing date


    【解决方案1】:

    有人可能会做得比这更好,因为它非常冗长:

    /(?:mon|tues?|weds|thurs?|fri|sat|sun), [a-z]{3,4} [0-9]{1,2}, [0-9]{4}/i
    
    $regex = '/(?:mon|tues?|weds|thurs?|fri|sat|sun), [a-z]{3,4} [0-9]{1,2}, [0-9]{4}/i';
    $string = 'Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here';
    
    preg_match($regex, $string, $matches);
    
    echo $matches[0];
    // Sun, Apr 09, 2000
    

    如果您预计日期会出现多次,那么稍作更改会有所帮助。

    // store the match as a named parameter called 'date'
    $regex = '/(?<date>(?:sun|mon|tues?|weds|thurs?|fri|sat|sun), [a-z]{3,4} [0-9]{1,2}, [0-9]{4})/i';
    
    $string = 'Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here. Sun, Mar 10, 2010';
    
    preg_match_all($regex, $string, $matches);
    
    print_r($matches['date']);
    /* 
     Array
        (
            [0] => Sun, Apr 09, 2000
            [1] => Sun, Mar 10, 2010
        )
    */
    

    从一天的名称开始,只是在偶然的情况下,您会得到一些看起来与一天相同但实际上并非如此的东西。

    我也不建议使用ereg(),因为它在 5.3.0 中已被弃用。请改用preg_match(),或使用其他preg_* 函数之一。

    【讨论】:

      【解决方案2】:

      不要依赖已弃用的ereg,而是尝试preg_match_all。

      $str = "Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here";
      
      preg_match_all('/.*([A-Za-z]{3,4}, [A-Za-z]{3,4} [\d]{1,2}, [\d]{4}).*/',$str,$matches);
      

      输出

      (
          [0] => Array
              (
                  [0] => Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here
              )
      
          [1] => Array
              (
                  [0] => Sun, Apr 09, 2000
              )
      
      )
      

      您将在 $matches[1] 中找到所有匹配项。

      【讨论】:

        【解决方案3】:

        这个正则表达式似乎适用于多种情况:

        $str = "Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here";
        $reg = '/(\w{3}),\s*(\w{3})\s*(\d+),\s*(\d{4})/';
        
        $match = preg_match($reg, $str, $matches);
        
        if ($match) {
            $date = "{$matches[2]} {$matches[3]} {$matches[4]}\n";
            // Apr 09 2000
            $timestamp = strtotime($date);
        }
        

        ereg() 不应再使用,从 PHP 5.3.0 开始,它已被弃用,而 preg 长期以来一直被视为更快、更广泛使用的替代方案。

        【讨论】:

          猜你喜欢
          • 2016-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          • 1970-01-01
          • 2017-06-07
          • 1970-01-01
          • 2014-04-09
          相关资源
          最近更新 更多