如何在 preg_match 之后在数据库中插入 2 位数字?
How to insert 2 digit number in database after preg_match?
preg_match_all('#(\d[wW]+|\d[dD]+|\d[hH]+|\s+)#i', $category, $matches); $hours=0; $days=0;
$hoursss=0;
foreach($matches[0] AS $match) {
switch(true) {
case preg_match('/\d[hH]+|\s+/', $match) :
$hours +=(int)$match;
break;
case preg_match('/\d[dD]+|\s+/', $match) :
$days +=(int)$match*24;
break;
case preg_match('/\d[wW]+|s+/', $match) :
$hoursss+= (int)$match*24*7;
break;
}
$case=$hours+$hoursss+$days;
}
当我输入 2d2w2h 时,它会转换为总小时数并插入数据库。
但是当我输入 20w20d20h 时,它在数据库中给出 0。
请帮帮我
你的第一个正则表达式是错误的(匹配句点字母前面的多个数字),它应该是:
#(\d+[wW]+|\d+[dD]+|\d+[hH]+|\s+)#i'
还要检查一些边缘情况的其他正则表达式,因为您处理它的方式并非完美无缺。
preg_match_all('#(\d[wW]+|\d[dD]+|\d[hH]+|\s+)#i', $category, $matches); $hours=0; $days=0;
$hoursss=0;
foreach($matches[0] AS $match) {
switch(true) {
case preg_match('/\d[hH]+|\s+/', $match) :
$hours +=(int)$match;
break;
case preg_match('/\d[dD]+|\s+/', $match) :
$days +=(int)$match*24;
break;
case preg_match('/\d[wW]+|s+/', $match) :
$hoursss+= (int)$match*24*7;
break;
}
$case=$hours+$hoursss+$days;
}
当我输入 2d2w2h 时,它会转换为总小时数并插入数据库。
但是当我输入 20w20d20h 时,它在数据库中给出 0。
请帮帮我
你的第一个正则表达式是错误的(匹配句点字母前面的多个数字),它应该是:
#(\d+[wW]+|\d+[dD]+|\d+[hH]+|\s+)#i'
还要检查一些边缘情况的其他正则表达式,因为您处理它的方式并非完美无缺。