我做一些PHP HTML解析,这是代码我现在所拥有的正则表达式的问题与多个结果
function get_tag($htmlelement,$attr, $value, $xml ,$arr) {
$attr = preg_quote($attr);
$value = preg_quote($value);
if($attr!='' && $value!='')
{
$tag_regex = '/<'.$htmlelement.'[^>]*'.$attr.'="'.$value.'">(.*?)<\\/'.$htmlelement.'>/si';
preg_match($tag_regex,$xml,$matches);
}
else
{
$tag_regex = '/'.$htmlelement.'[^>]*"(.*?)\/'.$htmlelement.'/i';
preg_match_all($tag_regex,$xml,$matches);
}
if($arr)
return $matches;
else
return $matches[1];
}
$htmlcontent = file_get_contents("doc.html");
$extract = get_tag('tbody','id', 'open', $htmlcontent,false);
$trows = get_tag('tr','', '', $htmlcontent,false);
拥有该行被解析/在$提取物中的内容都可以在这里http://pastebin.com/ydiAdiuC查看。
基本上,我正在阅读html内容并从html获取标签tbody。现在我想要在tbody中获取每个tr和td值并将其用于我的页面。任何想法如何使用,我想我没有使用正确的方法来实现preg_match_all。
相关答案:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – arnemart 2011-06-16 12:53:46