PHP正则表达式问题在数据库中使用


PHP regex question for use in DB

我是新手,有一个问题。我有两个带时间的输入框。我得到的值是3h 20m 31s。我如何把这个变成3:20:31,这样我就可以在我的数据库中使用它?

无需使用正则表达式。如果你有固定的格式你可以直接输入

$input = "3h 20m 31s";
$input = str_replace("h ",":",$input);
$input = str_replace("m ",":",$input);
$input = str_replace("s","",$input);

或者如果您热衷于正则表达式:

$input = "3h 20m 31s";
$regex = "/^'D*('d+)'D*('d+)'D*('d+)'D*$/";
$matches = array();
preg_match($regex, $input,$matches);
echo implode(":",array_slice($matches,1))