Php 检索';a';两个标签之间的标签

Php 检索';a';两个标签之间的标签,php,dom,tags,Php,Dom,Tags,我想要的是检索特定标记之间的HTML标记的数量 这个例子就是我所拥有的,但我不知道如何把其余的代码 $dom = new DOMDocument(); $dom->loadHTML($text); $i = 0; foreach($dom->getElementsByTagName("td") as $node){ //Retrieve every TD tag that have the attribute bgcolor = #0051AB //<td bgcol

我想要的是检索特定
标记之间的HTML
标记的数量

这个例子就是我所拥有的,但我不知道如何把其余的代码

 $dom = new DOMDocument();
 $dom->loadHTML($text);
 $i = 0;
 foreach($dom->getElementsByTagName("td") as $node){
 //Retrieve every TD tag that have the attribute bgcolor = #0051AB
//<td bgcolor=#0051AB> NODEVALUE </td>
   if($node->getAttribute("bgcolor") == "#0051AB"){
     $cat[]= $node->nodeValue;
   }
//HERE identify every 'a' html tag that are between the $node and the next one!!
//<a href="path">nodeValue</a>


 }
谢谢你的建议。

我更喜欢这个要求,而不是PHP DOM;为什么?这是不同的讨论

下面是您问题的解决方案

下载QueryPath并将其包含在PHP文件中

require("../../QueryPath\QueryPath.php");
下面是解析HTML的示例

$text="<body>
<table><tr><td bgcolor=#0051AB>Project 1</td></tr></table>
<a>link1</a>
 other tags and text..
<a>Link 2</a>
enter code here
<table><tr><td >Project 2</td></tr></table>
codecodecode
<a> Should Not Be Included</a>
codecodecode
<table><tr><td bgcolor=#0051AB>Project 2</td></tr></table>
codecodecode
<a>link3</a>
codecodecode</body>";
我更喜欢这个需求而不是PHPDOM;为什么?这是不同的讨论

下面是您问题的解决方案

下载QueryPath并将其包含在PHP文件中

require("../../QueryPath\QueryPath.php");
下面是解析HTML的示例

$text="<body>
<table><tr><td bgcolor=#0051AB>Project 1</td></tr></table>
<a>link1</a>
 other tags and text..
<a>Link 2</a>
enter code here
<table><tr><td >Project 2</td></tr></table>
codecodecode
<a> Should Not Be Included</a>
codecodecode
<table><tr><td bgcolor=#0051AB>Project 2</td></tr></table>
codecodecode
<a>link3</a>
codecodecode</body>";

简单的HTMLDOM易于使用


简单的HTMLDOM易于使用


xpath
//td[@bgcolor=“#0051AB”]//a
?你能澄清一下你在这里说的话吗?你能详细说明一下或者分享一下你的例子吗?这个例子现在在问题中
//td[@bgcolor=“#0051AB”]//a
?你能澄清一下你在这里说的话吗?你能详细说明一下或者分享一下你的例子吗?这个例子现在有问题了,这只是一个想法。PHP DOMdocument也可以用来实现这一功能,但由于jquery语法更为熟悉,因此querypath更可取。这只是一个实现这一功能的想法。PHP DOMdocument也可用于实现此功能,但由于对jquery语法更熟悉,因此最好使用querypath。
 $tags=htmlqp($text,'body')->children();
 $isRequiredTag=false;
 $i=0;
 foreach($tags as $pr)
 {
 $tag= $pr->tag();
 if($tag=='table'){
 $isRequiredTag= (htmlqp($text,$tag)->eq($i)->find('td')-  >attr('bgcolor')=='#0051AB')?"TRUE":"FALSE";
 $i++;
 }

 if ($isRequiredTag=="TRUE" && $tag=='a') echo $pr->text();

 } 
foreach($html->find('td') as $td) {
       $td_value = $td->plaintext;
      foreach($td->find('a') as $anchor) {
            ...
      }
}