簡體   English   中英

preg_match_all 查找兩個單詞之間的文本,打印

[英]preg_match_all find text between two words, print

我想獲取所有 vip 的 html 類,並打印每個 vip 結果

$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;
}

我將DOMDocumentDOMXPath結合使用:

$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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM