Q:

Convert string to same format

I have the five following strings (they are supposed to act like time):
11-20
9:30-20:00
8-20
7-16,30
9.30-9.50
I wonder if there is any way to convert them all to the format HH.HH-MM-MM.
For example 11-20 should be converted to 11.00-20.00.

A:

How about this:
$str = '11-20 9:30-20:00 8-20 7-16,30 9.30-9.50';
$result = preg_replace_callback('~([\d:,.]+)~', function($i) {
    $i[1] = str_replace(array(',', '.'), ':', $i[1]);
    if (strpos($i[1], ':') === false) {
        $i[1] .= ':00';
    }

    return $i[1];
}, $str);
var_dump($result);

http://codepad.viper-7.com/2esU4N

