dousi2013 2012-12-15 01:39
浏览 76
已采纳

php regex preg_match_all无法正常工作

Im using preg_match_all to check dates and words that starts with capital, the problem is on the dates because on the regex tester its telling me that this regex is fine but in the php script its not doing it correctly, my pattern is this:

$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]+)*#";

and I want it to match this: "12.10.1990" as well as "12.10.90"

thanks for your help!

  • 写回答

1条回答 默认 最新

  • dongxibeng5324 2012-12-15 01:47
    关注
    $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);
    

    Use this pattern for the date matching portion of your regex. Regardless you are trying to re-invent the wheel. There are built in PHP functions that can help you better determine if a date is valid or not.

    Use explode() and then put each segment into this function for example:

    $string = '12.10.1990';
    //$string = '12.10.90';
    
    $string = explode('.', $string);
    var_dump(checkdate($string[0], $string[1], $string[2]));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入