2017-01-19 43 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查询谓词。 –

相关问题