dongxiaofa6359 2014-08-18 12:18
浏览 18
已采纳

如何正确添加和OR语句

I am trying to do a comparison of serial numbers like so 20140831-123 or 20140831-1234 so the form can accept our new serial numbers which contain 4 last numbers. So far I have tried an elseif statement and an or operator with no results what am I doing wrong? is there a way to change the reg expression itself to accept 3 or 4 digits at the end of the serial?

if($name == 'newserial1'){
        $newserial1 = $_POST['newserial1'];

        if($newserial1 != '') {
            if(!preg_match('/^([0-9]{8}-)([0-9]{3})$/', $newserial1) ||
             (!preg_match('/^([0-9]{8}-)([0-9]{4})$/', $newserial1))) {
                $result['valid'] = false;
                $result['reason'][$name] = 'Incorrect Serial Number.';
            }
        }
    }
  • 写回答

3条回答 默认 最新

  • donljt2606 2014-08-18 12:19
    关注

    Just use the below regex to match last 3 or 4 digits also,

    ^([0-9]{8}-)([0-9]{3,4})$
    

    DEMO

    Explanation:

    • ^ Asserts that we are at the start.
    • ([0-9]{8}-) Captures 8 digits and a following - symbol.
    • ([0-9]{3,4}) Remaining three or four digits are captured by the second group.
    • $ Asserts that we are at the end.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?