使用PHP DOM文档,按类选择HTML元素并获取文本

问题描述:

我试图通过使用具有以下HTML(相同结构)和以下代码的PHP DOM元素,从div class = 'review-text'中获取文本。

I trying to get text from div where class = 'review-text', by using PHP's DOM element with following HTML (same structure) and following code.

然而,这似乎并不奏效
$ b

However this doesn't seem to work


  1. HTML p>

  1. HTML

$html = '
    <div class="page-wrapper">
        <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
            <article class="review clearfix">
                <div class="review-content">
                    <div class="review-text" itemprop="reviewBody">
                    Outstanding ... 
                    </div>
                </div>
            </article>
        </section>
    </div>
';


  • PHP代码

  • PHP Code

        $classname = 'review-text';
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $xpath     = new DOMXPath($dom);
        $results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
    
        if ($results->length > 0) {
            echo $review = $results->item(0)->nodeValue;
        }
    


  • 在此博客中提供了按类别选择元素的XPATH语法

    The XPATH syntax to select element by Class is provided at this Blog

    我尝试了很多来自*的在线教程示例,但似乎没有任何效果。我错过了什么?

    I have tried many example from *, online tutorials, but none seems to work. Am I missing something ?

    以下XPath查询完成您想要的功能。只需将提供给$ xpath-> query的参数替换为以下内容即可:

    The following XPath query does what you want. Just replace the argument provided to $xpath->query with the following:

    //div[@class="review-text"]
    

    编辑:
    为了便于开发,您可以测试自己的XPath查询的在线 http://www.xpathtester.com/test

    编辑2 :
    测试了这段代码;它完美地工作。

    Tested this code; it worked perfectly.

    <?php
    
    $html = '
        <div class="page-wrapper">
            <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
                <article class="review clearfix">
                    <div class="review-content">
                        <div class="review-text" itemprop="reviewBody">
                        Outstanding ... 
                        </div>
                    </div>
                </article>
            </section>
        </div>
    ';
    
    $classname = 'review-text';
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@class='" . $classname . "']");
    
    if ($results->length > 0) {
        echo $review = $results->item(0)->nodeValue;
    }
    
    ?>