2017-02-04 55 views
0

我想使用preg_match當前只是爲了檢索1值(在我移動到檢索多個值之前),但是,我沒有運氣。當我執行print_r()時,我的數組中沒有任何內容存儲。PHP - 使用preg_match從網頁上刮掉DIV元素

這裏是我的代碼我試圖至今:

<?php 
$content = '<div class="text-right font-90 text-default text-light last-updated vertical-offset-20"> 
    Reported ETA Received: 
    <time datetime="2017-02-02 18:12">2017-02-02 18:12</time> 
    UTC 
</div>'; 
preg_match('|Reported ETA Received: <time datetime=".+">(.*)</time>(.*)\(<span title=".+">(.*)<time datetime=".+">(.*)</time></span>\)|', $content, $reported_eta_received); 

if ($reported_eta_received) { 
    $arr_parsed['reported_eta_received'] = $reported_eta_received[1]; 
} 
?> 

所需的輸出:

2017-02-02 18:12 

我的上述代碼是行不通的。任何關於這方面的幫助將不勝感激。提前致謝。

+0

結帳DOM文檔http://stackoverflow.com/questions/10366458/php-dom-parsing-a-html-list-into-an-array – Nitin

回答

1

它可能不匹配,因爲在Reported ETA Received:和<time>標記之間有一條新的線。而你剛剛在那裏放置了一個空間(用[\ n \ r \ s \ t] +代替「」)。

而且,你爲什麼不乾脆用:

preg_match('|<time datetime=".*?">(.*?)</time>|', $content, $reported_eta_received);

您還可以使用:?P<name>一個更容易指向(聯想VS數字:如果你把更多的捕捉組數字可以改變)。

preg_match('|<time datetime=".*?">(?P<name>.*?)</time>|', $content, $match); print_r($match); // $match['name'] should be there if matched.

+0

由於它的正常工作。 – Faisal