【问题标题】:php regex preg_match_all not working correctlyphp regex preg_match_all 无法正常工作
【发布时间】:2012-12-15 01:39:27
【问题描述】:

我使用 preg_match_all 检查以大写开头的日期和单词,问题出在日期上,因为在正则表达式测试器上它告诉我这个正则表达式很好,但在 php 脚本中它没有正确执行,我的模式是这样:

$pattern = "#(((0[1-9]|[12][0-9]|3[01])([\/\.\\\-])((0[1-9]|1[012])\11)?)(\d\d\d\d|\d\d))+|([A-Z][a-z]+)(\s[A-Z][a-z]+)*#";

我希望它匹配这个:“12.10.1990”以及“12.10.90”

感谢您的帮助!

【问题讨论】:

标签: php regex preg-match preg-match-all


【解决方案1】:
$string = '12.10.1990 as well as 12.10.90';

preg_match_all('/[01]\d\.[0-3]\d\.\d{2,4}/', $string, $match);

print_r($match);

将此模式用于正则表达式的日期匹配部分。不管你是想重新发明轮子。内置 PHP 函数可以帮助您更好地确定日期是否有效。

使用explode(),然后将每个段放入这个函数例如:

$string = '12.10.1990';
//$string = '12.10.90';

$string = explode('.', $string);
var_dump(checkdate($string[0], $string[1], $string[2]));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2021-08-16
    • 2012-10-28
    • 2013-10-13
    相关资源
    最近更新 更多