2016-11-07 9 views
0

私は苦労している正規表現の助けが必要です。正規表現を変換する正規表現が置き換えられます。 with:

9.15-11.15 

へ:

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- 11:15 "しかし。

問題が私が作っているか、preg_replaceを使っているのか分からないのですか?あなたはそのためのregexが必要です場合

+1

あなたが ' '$ 1を使用することを意味するか:$ 2 - $ 3:代わりに$ 4''? '$ 0'はマッチ全体を返します。これはあなたが望むものではありません。 – Terry

+0

なぜstr_replace()関数の代わりに正規表現を使用していますか? – felipsmartins

答えて

1

を楽しむ:

$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');