简体   繁体   English

选择多个类名,并使用PHP DOMXpath在该类中获取子节点

[英]Choose multiple class name and get childnode inside that class with PHP DOMXpath

<div id="conti">
  <div class="no_matter"></div>
  <div class="row-0">
    <b></b>
    <span>
      <i>"child node that i want to get"</i>
    </span>
  </div>

  <div class="row-1">
    <b></b>
    <span>
      <i>"child node that i want to get"</i>
    </span>
  </div>

  <div class="row-0">
    <b></b>
    <span>
      <i>"child node that i want to get"</i>
    </span>
  </div>

  <div class="row-1">
    <b></b>
    <span>
      <i>"child node that i want to get"</i>
    </span>
  </div>

  ...
  ...
  class row-0 and row-1 repeats itself
  ...
  ...

</div>

This is the HTML that i want to parse and get contents. 这是我要解析和获取内容的HTML。 I want text node inside <i> tag . 我希望text node inside <i> tag I am using DOMDocument and DOMXpath 我正在使用DOMDocumentDOMXpath

$dom = new DOMDocument();
$dom->loadHTMLFile('http://www.meal.org/anter.php');
$dom->preserveWhiteSpace = true;

$xpath = new DOMXPath($dom);

$row = $xpath->query('//*[@class="row-0" ]');  //my problem begins there. I want both 'row-0' and 'row-1'. How i am gonna choose multiple class?

//and than how i am gonna get `<i>` tag inside every `row-0` and `row-1` class and get the text node?

You can do all that with the following XPath query: 您可以使用以下XPath查询执行所有操作:

//*[starts-with(@class,"row-")]/span/i/text()

MDN on starts-with : MDN on starts-with

The starts-with checks whether the first string starts with the second string and returns true or false. starts-with检查第一个字符串是否以第二个字符串开头并返回true或false。

If you are interested in all text nodes in these rows, so also the ones in the b tags, and any other tags that might be in those rows, then use the double slash: 如果您对这些行中的所有文本节点感兴趣,那么b标记中的文本节点以及可能在这些行中的任何其他标记也会感兴趣,然后使用双斜杠:

//*[starts-with(@class,"row-")]//text()
$iTags = $xpath->query('//div[@class="row-0" or @class="row-1"]/span/i');

foreach ($iTags as $iTag) {
    var_dump(trim($iTag->nodeValue));
}

I'm not fammiliar with XPath, so I'm looping trough each <div> element using DOMDocument() . 我不熟悉XPath,所以我使用DOMDocument()循环遍历每个<div>元素。 Check wheter it has a attribute class with value row-0 or row-1. 检查它是否具有值为row-0或row-1的属性类。 If so then get each element <i> and dump the nodeValue. 如果是,则获取每个元素<i>并转储nodeValue。

foreach($dom->getElementsByTagName('div') as $div){
    if($div->getAttribute('class') == 'row-0' OR $div->getAttribute('class') == 'row-1'){
        foreach($div->getElementsByTagName('i') as $i){
            var_dump($i->nodeValue);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM