Php 具有多个结果的正则表达式问题

Php 具有多个结果的正则表达式问题,php,regex,dom,Php,Regex,Dom,我正在做一些php html解析,这是我现在拥有的代码 function get_tag($htmlelement,$attr, $value, $xml ,$arr) { $attr = preg_quote($attr); $value = preg_quote($value); if($attr!='' && $value!='') { $tag_regex = '/<'.$htmlelement.'[^>]*'.$att

我正在做一些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);
函数get_标记($htmlelement、$attr、$value、$xml、$arr){
$attr=预报价($attr);
$value=预报价($value);
如果($attr!=''&&$value!='')
{
$tag_regex='/]*'.$attr'='.$value'.'>(.*)/si';
preg_match($tag_regex,$xml,$matches);
}
其他的
{
$tag_regex='/'.$htmlelement.[^>]*“(.*?\/'.$htmlelement./i';
preg_match_all($tag_regex,$xml,$matches);
}
如果($arr)
返回$matches;
其他的
返回$matches[1];
}
$htmlcontent=文件获取内容(“doc.html”);
$extract=get_标记('tbody','id','open',$htmlcontent,false);
$trows=get_标记('tr','',$htmlcontent,false);
必须解析的行/$extract中的内容可以在此处查看

基本上,我是在阅读html内容并从html中获取标记tbody。现在我想在tbody中获取每个tr和td值并在我的页面中使用。如果知道如何使用,我认为我没有使用正确的方法来实现preg_match_all。

请使用PHP,而不是正则表达式

快速方法:

  • 在HTML中加载
  • 获取
    t车身
    标签
  • 获取内的
    tr
    标记

相关回答:你能给我一个简短的代码吗?html标记没有正确关闭,我无法控制html内容。@joza:如果它完全损坏,请先检查一下。否则告诉DomDocument忽略错误。@joza,无效的标记将是一个问题。请参阅hakre的评论以了解解决方法。无效的标记将是一个很小的问题tmare用于正则表达式,这也是它们在解析HTML时遇到困难的主要原因之一。