Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHPDOM文档,按类选择HTML元素并获取其文本_Php_Html_Domdocument - Fatal编程技术网

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

使用PHPDOM文档,按类选择HTML元素并获取其文本,php,html,domdocument,Php,Html,Domdocument,我试图通过使用PHP的DOM元素和下面的HTML(相同的结构)以及下面的代码从div中获取文本,其中class=“review-text” 但是这似乎不起作用 HTML $html = ' <div class="page-wrapper"> <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">

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

但是这似乎不起作用

  • 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>
    ';
    
  • 此处提供了按类选择元素的XPATH语法


    我试过许多StackOverflow的例子,在线教程,但似乎都不管用。我遗漏了什么吗?

    下面的XPath查询满足您的要求。只需将提供给$xpath->query的参数替换为以下内容:

    //div[@class="review-text"]
    
    编辑: 为了便于开发,您可以在线测试自己的XPath查询

    编辑2: 测试了这个代码;它工作得很好

    <?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;
    }
    
    ?>
    
    在答案上展开,也可以使用在特定范围内搜索。这可以通过将
    contextNode
    作为第二个参数传递给
    DomXpath->query
    方法来实现:

    $dom = new DOMDocument;
    $dom->loadHTML ($html);
    $xpath = new DOMXPath ($dom);
    
    foreach ($xpath->query ("//section[@class='page single-review']") as $section)
    {
        // search for sub nodes inside each element
        foreach ($xpath->query (".//div[@class='review-text']", $section) as $review)
        {
            echo $review->nodeValue;
        }
    }
    
    请注意,在节点内部搜索时,需要通过在表达式开头添加点
    来使用相对路径:

    "//div[@class='review-text']" // absolute path, search starts from the root element
    ".//div[@class='review-text']" // relative path, search starts from the provided contextNode
    

    //div[contains(@class,'review text')]
    我确实尝试了您的XPath查询,但似乎也不起作用。问题是否在于查询XPath之前的代码语法。
    "//div[@class='review-text']" // absolute path, search starts from the root element
    ".//div[@class='review-text']" // relative path, search starts from the provided contextNode