dtz30833 2011-01-18 15:04
浏览 43
已采纳

定义日期格式,正则表达式匹配和检查有效性 - 同行评审

I'm writing a input validation method for codeigniter to check date format. I'm actually just doing it in a test script to get the functionality nailed down. I've got something that works but I'd just like see if I'm approaching this the best (or worst) way.

Really specifically I'm looking at the second half, I've commented to point out what I mean.

<?

$input = $_POST['input'];   //text input of intended format
$date  = $_POST['date'];    //text input of date in matching format
                            //examples: y-m-d, Y.M.D, m/D/Y  (Case has no affect)

//I'm setting up a regex string based on given format
$pattern = preg_replace('/[yY]/','([0-9]{4})',$input);
$pattern = preg_replace('/[mM]/','([0-9]{1,2})',$pattern);
$pattern = preg_replace('/[dD]/','([0-9]{1,2})',$pattern);

//escaping slashes (if used as date delimiter)
$pattern = str_replace('/','\/',$pattern);


echo "Format  : " . $input . "<br />";
echo "Date    : " . $date . "<br/>";
echo "============" . "<br />";
echo "<br/>";

//if given date matches given format
if(preg_match('/^'.$pattern.'$/',$date,$matches)) {
    echo 'YAY A MATCH! <br/>';

    //From here down seems like it could be improved, seems a bit brute force
    //All of this below, is trying to get the order of the format so I can feed the proper values
    //to the checkdate() function to check date validity.

    preg_match('/[yY]/', $input, $match_year,PREG_OFFSET_CAPTURE);
    preg_match('/[mM]/', $input, $match_month,PREG_OFFSET_CAPTURE);
    preg_match('/[dD]/', $input, $match_day,PREG_OFFSET_CAPTURE);

    if ($match_year[0][1] < $match_month[0][1] && $match_year[0][1] < $match_day[0][1]) {
        $year = $matches[1];
        array_splice($matches,1,1);
    }
    else if ($match_year[0][1] > $match_month[0][1] && $match_year[0][1] > $match_day[0][1]) {
        $year = $matches[3];
        array_splice($matches,3,1);
    }
    else {
        $year = $matches[2];
        array_splice($matches,2,1);
    }

    if ($match_month[0][1] < $match_day[0][1]) {
        $month = $matches[1];
        $day   = $matches[2];
    }
    else {
        $month = $matches[2];
        $day   = $matches[1];
    }

    echo "<br/>";
    echo "<br/>";
    echo $month . ' / ' . $day . ' / ' . $year . "<br/>";

    if (checkdate($month,$day,$year)) { 
        echo "This is a valid date."; 
    } 
    else { 
        echo "This is not a valid date"; 
    } 
} 
else {
    echo "Given date does not match given format"; 
}
  • 写回答

2条回答 默认 最新

  • dongmiyu8979 2011-01-18 15:41
    关注

    Why not just do it all in one regex using named subpatterns?

    $search = array(
        '/[yY]/',
        '/[mM]/',
        '/[dD]/',
    );
    $replace = array(
        '(?P<year>[0-9]{4})',
        '(?P<month>[0-9]{1,2})',
        '(?P<day>[0-9]{1,2})',
    );
    $pattern = preg_replace($search, $replace, $input);
    

    Then, just run it against the input:

    if (preg_match('/' . $pattern . '/', $date, $match)) {
        $year = $match['year'];
        $month = $match['month'];
        $day = $match['day'];
    } else {
        echo "Date not in proper format";
    }
    

    But in general depending on your needs I'd just use strtotime or date_parse_from_format...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置