regex/php:从字符串解析时间?

stu*_*man 1 php regex

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

示例字符串:"这是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

谢谢你的回复!!

zom*_*bat 6

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

$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 )
/*
Run Code Online (Sandbox Code Playgroud)