正则表达式从字符串中获取日期

时间:2012-01-21 21:17:55

标签: php regex parsing date

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

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

在这个例子中:

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]";
}

也有兴趣听取非正则表达式的解决方案。

3 个答案:

答案 0 :(得分:2)

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

/(?: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_*个功能。

答案 1 :(得分:1)

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

$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一直被视为更快,更广泛使用的替代品。

答案 2 :(得分:1)

不要依赖已弃用的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]找到所有匹配项。