簡體   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