preg_match php 日期

preg_match for php for the date

如何使用php到preg_match以下情况的字符串?

字符串将类似于 20150923_1111。这种情况下的日期格式为 Ymd_Hi.

只是想知道是否可以验证日期格式。

我只需要确保字符串是数字且长度相同,而不必验证实际时间。

听起来您希望正则表达式检查字符串是否为 8 位数字,后跟下划线,然后是 4 位数字。所以,你可以像这样使用 preg_match

if (preg_match('/^[0-9]{8}_[0-9]{4}$/', $input)) {
  // Process here
}

^表示字符串的开始,[0-9]{x}表示查找连续的x位数0-9,$代表字符串的结尾。