我要像12小時34米45S一個輸出轉向十二時34分45秒PHP正則表達式輸出轉換
此外,如果一個O這些返回空應該是可能是將忽略它。 因此,34m 45s應該是00:34:45並且當然可以使用單個數字,例如1h 4m 1s以及組合單個和兩個數字,例如12h 4m 12s等等。
有人可以幫忙嗎?
這是實際的代碼
$van = $_POST['gespreksduur_van']; $tot = $_POST['gespreksduur_tot']; $regex = '/(\d\d?h ?)?(\d\d?m ?)?(\d\d?s)?/';
if(preg_match($regex, $van, $match) AND preg_match($regex, $tot, $matches))
{
for ($n = 1; $n <= 3; ++$n) { if (!array_key_exists($n, $match)) $match[$n] = 0; }
for ($i = 1; $i <= 3; ++$i) { if (!array_key_exists($i, $matches)) $matches[$i] = 0; }
$van = printf("%02d:%02d:%02d", $matches[1], $matches[2], $matches[3]);
$tot = printf("%02d:%02d:%02d", $match[1], $match[2], $match[3]);
print($van);
print($tot);
$data['gespreksduurvan'] = htmlspecialchars($van);
$data['gespreksduurtot'] = htmlspecialchars($tot);
$smarty->assign('gsv',$data['gespreksduurvan']);
$smarty->assign('gst',$data['gespreksduurtot']);
}
除了我的提供的答案,都沒有爲這個字符串'1h 45m'工作。也許有人可以更新他們的正則表達式的答案,以便這個字符串也通過。 – danishgoel
@danishgoel,我更新的答案有效。在我的原創中,我只是在正則表達式中忘記了一個問號,意外地需要秒數。現在它正在工作 –
define * empty *,是'hms'可以接受嗎? – hakre