2015-10-20 21 views
2

我需要關於如何更改當前RegEx行的一些指示。使用RegEx從字符串獲取(解析)時間

例字符串: 9:00 AM - 9:30 AM

希望的輸出: 900 930(其每一個可以在一個陣列或任何一個索引)..

電流的方法:

preg_match('/^([0-9]{1,2}):([0-9]{1,2}) ?([ap]m)/i', trim($presentationtime), $matches); 

但是,這似乎只是讓我第一(剝離/解析)的時間。

結果:

$presentationtime = $matches[1] . $matches[2]; 

echo 'Matches check: '. $matches[0] . '<br>'; 
echo 'Matches check: '. $matches[1] . '<br>'; 

回報:

匹配檢查:9

匹配檢查:00

我怎樣才能改變我的正則表達式來獲得兩次(剝離/解析的同樣的方式)..

我期待一個6索引數組......但只能ge TA 3索引/計數陣列這樣做的

+2

使用'preg_match_all' – anubhava

+1

爲什麼要使用正則表達式都在這裏嗎?你可以簡單地在' - '上爆炸來獲取兩個不同的時間分量。然後,您可以將這些時間組件讀取到'DateTime'或類似的對象中,並將輸出字符串格式化爲任何您想要的。輸入字符串的變體中是否存在更復雜的問題,您不會顯示這將暗示正則表達式是更好的方法? –

回答

2

爲@anubhava說,使用preg_match_all

$presentationtime = '9:00 AM - 9:30 AM'; 
preg_match_all('/([0-9]{1,2}):([0-9]{1,2}) /', trim($presentationtime), $matches); 

print_r($matches); 

結果:

Array 
(
    [0] => Array 
     (
      [0] => 9:00 
      [1] => 9:30 
     ) 

    [1] => Array 
     (
      [0] => 9 
      [1] => 9 
     ) 

    [2] => Array 
     (
      [0] => 00 
      [1] => 30 
     ) 

) 

編輯回答評論:

很懶惰的解決辦法得到一個二維數組,正則表達式平庸

$presentationtime = '9:00 AM - 9:30 PM'; 
preg_match('/([0-9]{1,2}):([0-9]{1,2})\s([apm]+)\s-\s([0-9]{1,2}):([0-9]{1,2})\s([apm]+)/i', trim($presentationtime), $matches); 

結果

Array 
(
    [0] => 9:00 AM - 9:30 PM 
    [1] => 9 
    [2] => 00 
    [3] => AM 
    [4] => 9 
    [5] => 30 
    [6] => PM 
) 
+0

有沒有一種方法,使/抓住它所有爲一(維)陣列? 我的模式適用於REGEX101.com但事實上並非如此..? 被張貼更新是在事實上溶需要的AM/PM的東西,以及(做一些24小時計算) – whispers

+0

@whispers看到我的編輯:) – Thesee

+0

沒有正則表達式: '$ presentationtime ='9:00 AM - 9:30 PM'; $ stringToDel = array('',':'); $ presentationtime = str_replace($ stringToDel,'',$ presentationtime); $ times = explode(' - ',$ presentationtime); 陣列 ( [0] => 900AM [1] => 930PM )' – Thesee

0

一種方式:

$data = '9:00 AM - 9:30 AM'; 
print str_replace('-', ' ', preg_replace("/[^0-9-]/","", $data)); 

Result: 
900 930 
  • 保持數字和破折號
  • 與空間

在前面的示例替換破折號,我用有限的正則表達式與preg_replace。爲了保持簡單和使用的時間與AM/PM,我只是爆炸(因爲你已經想到了),

$data = '9:00 AM - 9:30 PM'; 
$timeArray = array_map('trim', explode('-', $data)); // trim each array item 
$timeArray = array_map('Time24Hr', $timeArray); // clean up time 
print_r($timeArray); 

function Time24Hr($time) { 
    $time_ampm = explode(' ', $time); 
    if (strtolower(trim($time_ampm[1])) === 'am') { 
     // A 
     return $time_ampm[0]; 
     // B 
     //return str_replace(':', '', $time_ampm[0]); 
     // C 
     //return $time; 
    } 

    $hr_min = explode(':', trim($time_ampm[0])); 

    // A 
    return ($hr_min[0] + 12) . ':' . $hr_min[1]; 
    // B 
    //return ($hr_min[0] + 12) . $hr_min[1]; 
    // C 
    //return ($hr_min[0] + 12) . ':' . $hr_min[1]. ' ' . $time_ampm[1]; 

} 

結果:

Result A: 
Array 
(
    [0] => 9:00 
    [1] => 21:30 
) 

Result B: 
Array 
(
    [0] => 900 
    [1] => 2130 
) 

Result C: -- obviously this is insane, but you can easily make it sane. This is just an example 
Array 
(
    [0] => 9:00 AM 
    [1] => 21:30 PM 
) 

當然,這種簡單的方法可以簡化更進一步。這個方法不需要regex。

+0

我用這個: ^([0-9] {1,2}):(? - )([0-9] {1,2})([AP]米)([0-9] {1,2}):([0-9] {1,2})([AP]米) 我發現我需要保持AM/PM做24小時(12)除了PM會話? @zedfoxus這是一個很好,乾淨的代碼行。我怎樣才能訪問每一次單獨? (和是否有更新,以保持AM/PM每次用 哦..我只注意到你的arent使用REGEX..I想我可以做同樣的和空間爆炸,並更換:?字符 – whispers

+0

@whispers在我以前的回答中,我只用'/ [^ 0-9 - ] /'的形式使用了一點正則表達式,在編輯的答案中我避免了使用正則表達式,我會寫這個沒有正則表達式,使其更易於維護的人接管該代碼在某些時候,無論他們的經驗水平。 – zedfoxus

1

你必須簡單地使用preg_match_all(由anubhava在註釋說明),並在正則表達式的開始刪除^

preg_match_all('/([0-9]{1,2}):([0-9]{1,2}) ?([ap]m)/i', trim($presentationtime), $matches); 

然後$matches將是這樣的:

Array 
(
    [0] => Array 
     (
      [0] => 9:00 AM 
      [1] => 9:30 AM 
     ) 

    [1] => Array 
     (
      [0] => 9 
      [1] => 9 
     ) 

    [2] => Array 
     (
      [0] => 00 
      [1] => 30 
     ) 

    [3] => Array 
     (
      [0] => AM 
      [1] => AM 
     ) 

) 

如果你想改進你可以使用的正則表達式:

preg_match('/([0-9]{1,2}):([0-5]\d)\s*([ap]m)/i', trim($presentationtime), $matches); 

分鐘部分將匹配00到59兩位數字,空間部分\s*是可選的,並匹配多個空白字符(空格,製表符,CR,FF ...)