2016-11-07 41 views
0

我需要一個正則表達式的幫助,我正在努力。正則表達式來轉換時間格式替換。與:

我想轉換:

9.15-11.15 

到:

9:15-11:15 

據我已經得到了:

$message = preg_replace('/([0-9]{1,2}+)\.([0-9]{1,2}+)\-([0-9]{1,2}+)\.([0-9]{1,2}+)/', '$0:$1-$3:$4', $message); 

這將返回「9.15-11.15:9- 11:15「。

我不是100%確定問題是我正在做的比賽還是我使用preg_replace?

+1

你的意思是使用''$ 1:$ 2- $ 3:$ 4''呢? '$ 0'將返回整個比賽,這不是你想要的。 – Terry

+0

爲什麼你使用正則表達式而不是str_replace()函數? – felipsmartins

回答

1

如果你需要一個regex

$message = 'Hello. This is some string with 9.15-11.15 time inside, and I don\'t want to replace the DOTS... inside'; 
$message = preg_replace('/(\d{1,2})\.(\d{1,2})\-(\d{1,2})\.(\d{1,2})/','$1:$2-$3:$4', $message); 
var_dump($message); 
// string(102) "Hello. This is some string with 9:15-11:15 time inside, and I don't want to replace the DOTS... inside" 
3
echo str_replace('.', ':', '9.15-11.15'); 

享受