dongpu3347 2014-07-31 22:17
浏览 85
已采纳

如何使用PHP检查给定格式的字符串?

I have many headlines in my project like:

00.00.2014 - Headline Description e.t.c.

I want to check with php if the given strings contain the format 00.00.0000 - in front. The part after the - doesn't matter.

Can someone help me with something like:

$format = '00.00.0000 -';

if ($string MATCHES $format IN FRONT) {
    // ...some code...
}
  • 写回答

3条回答 默认 最新

  • douxian5963 2014-07-31 22:39
    关注

    This should work:

    if (preg_match("/^\d{2}\.\d{2}\.\d{4}\s\-\s.*$/", $string) === 1) {
        // $string matches!
    }
    

    Explanation:

    • ^ is "the beginning of the string"
    • \d is any digit (0, 1, 2, ..., 9)
    • {n} means "repeated n times"
    • \. is a dot
    • \s is a space
    • \- is a minus sign
    • . is "any single character"
    • * means "repeated 0 or more times`
    • $ means "end of the string"
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失