preg_match_all在两个单词之间找到文本,打印

时间:2017-01-19 02:24:10

标签: php preg-match-all

我希望得到所有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;
}

1 个答案:

答案 0 :(得分:1)

我会将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;
}