2017-01-19 84 views
1

我想那是VIP的所有HTML類,並打印出每個VIP結果preg_match_all找到兩個詞之間的文本,打印

$text = "<enty> hello baby, we love osama:) That means 2016 set a global heat record<enty2016>for the third year in a row</enty2016>according to NOAA and NASA, who held a joint press conference on Wednesday to discuss the record. <endentry> <enty2016>Temperatures over the Earth's continents and oceans in 2016 were 1.1 degree Celsius (1.98 degrees Fahrenheit) </enty2016>above the pre-industrial average, according to the WMO. That means we are already a <endline>majority of the way to the 1.5-degree warming goal <endenty> "; 

我想打印在enty2016s

preg_match("/<enty2016>(.*?)<\/enty2016>/is", $html, $matches); 
foreach($matches[1] as $enty2016s){ 
echo $enty2016s; 
} 
+1

使用DOM,而不是正則表達式。 –

+0

somtimes我不是在搜索html – newuser0250

+0

你的例子是html,如果你有其他的例子,你應該發佈它們。 – nogad

回答

1

我的所有數據將使用DOMDocument結合DOMXPath

$dom = new DOMDocument; 
$dom->loadHTML($html); 

$xp = new DOMXPath($dom); 
$divNodeList = $xp->query('//div[@class="vip"]/text()'); 

foreach ($divNodeList as $divNode) { 
    echo $divNode->nodeValue . PHP_EOL; 
} 
+0

如果班級名稱中有換行符,該怎麼辦? data-vip-url =「/ v.index」 class =「 search-item regular-ad – newuser0250

+0

www.kijiji.ca/b-cars-vehicles/british-columbia/ford-carproof/k0c27l9007 – newuser0250

+1

html class names沒有換行符或空格,包含幾個類名的類屬性可以有空格,在這種情況下,使用'contains'函數改變xpath查詢謂詞。 –

相關問題