Regular Expression to get contents of div class in php
Possible Duplicate:
PHP DOMDocument, finding specific tags
How to parse and process HTML with PHP?
Is there any special syntax to get nested div element with unique class name in RegExp in php. Consider I have a syntax like
<div style="demo">
<div class="row">
<div title="abc@examples.com" class="text">ABC</div>
</div>
<div class="row">
<div title="pqr@examples.com" class="text">PQR</div>
</div></div>
here how can we retrieve all emailids using RegExp and preg_match_all().
preg_match_all("/<div title="(.*)" class="text">/", $subject, $matches);
If the emails is the only data you want there are better regexps for matching emails only. See http://fightingforalostcause.net/misc/2006/compare-email-regex.php
Regex are bad at parsing HTML. Use a DOM Parser and this XPath:
//div[@style="demo"]/div[@class="row"]/div[@class="text"]/@title
If class="text"
is exclusive to the divs you want to match, you can also do
//div[@class="text"]/@title
Also see:
<?php
$html = '<div style="demo">
<div class="row">
<div title="abc@examples.com" class="text">ABC</div>
</div>
<div class="row">
<div title="pqr@examples.com" class="text">PQR</div>
</div></div>
';
$doc = DOMDocument::loadHTML($html);
$xpath = new DOMXPath($doc);
foreach($xpath->query('//div[@style="demo"]/div[@class="row"]/div[@class="text"]/@title') as $div){
echo $div->value . PHP_EOL;
}
这假设类的属性完全是那些(逐字),但我希望你明白这一点。
链接地址: http://www.djcxy.com/p/29908.html上一篇: 使用PHP中的URL获取元素的特定内容块
下一篇: 正则表达式在php中获取div类的内容