【问题标题】:PHP dom to get tag class with multiple css class namePHP dom获取具有多个css类名的标签类
【发布时间】:2011-06-17 15:27:18
【问题描述】:

我很难获得第二个链接 href 和 Text。如何选择 class="secondLink SecondClass"。使用 PHP Dom,谢谢

        <td class="pos" >
            <a class="firstLink" href="Search/?List=200003000112097&sr=1" >
                Firs link value
            </a>

            <br />

            <a class="secondLink SecondClass" href="/Search/?KeyOpt=ALL" >
                Second Link Value
            </a>
        </td

我的代码是

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

// grab all the on the page

$xpath = new DOMXPath($dom);
//$hrefs = $xpath->evaluate("/html/body//a[@class='firstLink']");// its working

$hrefs = $xpath->evaluate("/html/body//a[@class='secondLink SecondClass']");// not working

谢谢

【问题讨论】:

  • 您的code is working。 XPath 是正确的。你做错了什么。
  • 哇......它的工作原理。为什么它在这里不起作用。谢谢
  • see左右切换类名时不起作用

标签: php css class dom


【解决方案1】:
 $hrefs = $xpath->evaluate("/html/body//a[contains(concat(' ',@class,' '),' secondClass ')
          and (contains(concat(' ',@class,' '),' secondLink '))]"

来自this answer

【讨论】:

    【解决方案2】:

    您可以通过选择具有类 pos 的 td 并选择锚标签来选择它。那么你就不能控制你的返回数组来获取你的特定锚标记

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2012-06-21
      相关资源
      最近更新 更多