2012-04-27 128 views
0

我有以下五個字符串(他們應該像時間): 11-20 9:30-20:00 8-20 7-16,30 9.30-9.50字符串轉換爲相同格式

我不知道是否有任何方式將它們全部轉換爲格式HH.HH-MM-MM。 例如11-20應轉換爲11.00-20.00。

回答

2

如何:

$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

1

建設什麼zerkms我纔想出了這個

<?php 
class timeString { 

public function timeString($string) { 
     $exp = explode('-', $string); 
     $timeString = array(); 
     foreach ($exp as $value) { 
      $timeString[]= $this->normTime($value); 
     } 
     $time = implode('-', $timeString); 
     return $time; 
    } 
public function normTime($string) { 
     $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]; 
       }, $string); 
     return $result; 
    } 
} 

這將返回你問究竟什麼11.00-20.00

祝你好運。

相關問題