繁体   English   中英

正则表达式/ PHP的:从字符串解析时间?

[英]regex/php: parse times from string?

从字符串解析时间的最佳方法是什么?

示例字符串:“这是10:45,这是10:48、10:49、10:50、22:15”;

预期收益:

[0] = 10:45

[1] = 10:48

[2] = 10:49

[3] = 10:50

[4] = 22:15

感谢您的任何答复!

这将提供所需的输出,并将小时/分钟数限制为小时的第一位置和分钟的第一位置的有效值:

$y = "this is 10:45 this is 10:48, 10:49, 10:50, 22:15";
preg_match_all("/(([0-1]\d|2[0-3]):[0-5]\d)/",$y,$matches);
print_r($matches[1]);
/*
Outputs:
Array ( [0] => 10:45 [1] => 10:48 [2] => 10:49 [3] => 10:50 [4] => 22:15 )
/*
preg_match_all('/\d{2}:\d{2}/', $string, $matches);

您将所有匹配项放入$matches数组中。

看起来很简单。 尝试将preg_match_all与“ / \\ d \\ d:\\ d \\ d /”一起使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM