我一個有一個字段值看起來像這樣的「24小時30米」爆炸我的CSV導入到數據庫的任務時間
我如何爆炸的這兩組數字的分開?以下是我迄今爲止
$tasktime = "24h 30m";
$pieces = explode("h ", $tasktime);
echo $pieces[0]; // want this to echo 24
echo $pieces[1]; // want this to echo 30
我一個有一個字段值看起來像這樣的「24小時30米」爆炸我的CSV導入到數據庫的任務時間
我如何爆炸的這兩組數字的分開?以下是我迄今爲止
$tasktime = "24h 30m";
$pieces = explode("h ", $tasktime);
echo $pieces[0]; // want this to echo 24
echo $pieces[1]; // want this to echo 30
$tasktime = "24h 30m";
$pieces = explode("h ", $tasktime);
$pieces[1] = rtrim($pieces[1], "m");
echo $pieces[0]; // want this to echo 24
echo $pieces[1]; // want this to echo 30
其實我不知道PHP,但這對我來說很有意義。
這工作。謝謝!
$tasktime = "24h 30m";
$pieces = array_map('intval', explode(' ', $tasktime));
好的謝謝Ayesh – Hector
如果它總是格式,你可以這樣做是爲了得到一個DateInterval對象。
$tasktime = "24h 30m";
$interval = new DateInterval('PT' . strtoupper(str_replace(' ', '', $tasktime)));
print_r($interval);
輸出
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 24
[i] => 30
[s] => 0
[invert] => 0
[days] =>
)
這裏是你怎麼可能把它寫成一個正則表達式:
$tasktime = "24h 30m";
preg_match('/(\d+)h (\d+)m/', $tasktime, $pieces);
echo $pieces[1]; // echoes 24
echo $pieces[2]; // echoes 30
他的言論似乎表明,他希望切斷信嗎? +1無論如何 –
@IsaacFife那裏不會是在我的解決方案中intval應用後的任何字母 – Gordon
啊,很好。本週我剛開始使用php。我應該保持閉嘴。 ^^ –