php中的month-regex问题.验证问题


month regex problem in php. validation problem

function preveriDatum($string)
{   
    if (preg_match("^(3)?(?(1)[0-1]|(0)?(?(2)[1-9]|[12][0-9]))'.(0)?(?(3)[1-9]|1[0-2])'.[1-9][0-9]{3}$^", $string)) 
        return true;
    else 
        return false;
}

有效期为2010年11月13日,但不适用于2010年1月13日。我可以让它在2010年1月1日有效吗?

天哪,你想在这里做什么?你能把它放到DateTime类中,然后用这种方式验证它吗?http://php.net/manual/en/book.datetime.php

对于正则表达式,在任何情况下,您都希望使用*而不是?对于0?要求至少有一个实例。而不是(0(?使用(0(*。

您可以使用:

if (preg_match("^(0?[1-9]|[12][0-9]|3[01])'.(0?[1-9]|1[012])'.([1-9][0-9]{3})$", $string)) 

当然,最好使用DateTime类(http://php.net/manual/en/book.datetime.php)。

.(0)?(?(3)[1-9]|1[0-2])更改为.(0)?(?(3)[1-9]|1[0-2]|[1-9])。还可以考虑使用日期库。