繁体   English   中英

如何<a>使用 xpath 在 PHP 中</a>获取所有<a>具有类名的元素并添加一个</a><p><a>在每个元素下标记?</a>

[英]How can I get all <a> elements with a class name in PHP using xpath and add a <p> tag under each of those elements?

我尝试使用 xpath 在 PHP 中获取所有类名为“post-title”的元素,但它不起作用。 我应该提到 a 元素是由短代码输出的,所以我什至不确定 xpath 是否可以在这种情况下工作。 我不知道为什么,但它在 $items 中返回一个长度为 0 的 DOMNodeList 对象。 我还想获得 post_title,它是 a 元素中的文本,因此我使用的是 $post_title = $item->nodeValue,但我不确定这是否可行。 非常感激任何的帮助。

    function add_rating_below_search_result_post_title(){
        $dom = new DomDocument;
        $dom->loadHTMLFile("example.com");
        $xpath = new DomXPath($dom);    
        $items = $xpath->query("//a[@class='post-title']");
    
        foreach ($items as $item) {
            $post_title = $item->nodeValue;
            $post_id = get_page_by_title($post_title, OBJECT, 'post');
            
            $rating_content = get_rating($post_id);
            
            $doc = new DOMDocument();
            $rating = $doc->createElement("p", $rating_content);
          
            $item->appendChild($rating);        
        }
    }

这是一项的示例 HTML。 HTML 中有多个这样的项目。 我想要的 a 元素位于底部的第 6 行。 我需要为每个项目获取与此模式匹配的所有元素。

<div class="cl-layout__item cl-layout__item--id-1234">
<div class="cl-layout__item-spacing">
<div class="cl-template cl-template--post cl-template--id-1234 cl-template--image-top">
<div class="cl-element cl-element-featured_media cl-element--instance-1234  cl-element-featured_media--sizing-natural">
<a class="cl-element-featured_media__anchor" href="https://www.example.com/item1/"><img data-pin-title="Item Search" class="cl-element-featured_media__image" src="https://cdn.shortpixel.ai/spai/w_300+q_lossless+ret_img+to_webp/https://www.example.com/wp-content/uploads/2020/07/Item1-300x300.jpg" data-spai="1" alt="Item 1" data-pin-nopin="true" data-spai-upd="339">
<noscript data-spai="1"><img data-pin-title="Item Search"  class="cl-element-featured_media__image"  src="https://cdn.shortpixel.ai/spai/q_lossless+ret_img/https://www.example.com/wp-content/uploads/2020/07/Item1-300x300.jpg" data-spai-egr="1"  alt="Item 1"  />
</noscript>
</a>
</div>
<div class="cl-element cl-element-section cl-element--instance-1222 ">
<h4 class="cl-element cl-element-title cl-element--instance-1333 ">
<a class="post-title" href="https://www.example.com/item1/">Item 1 post title</a>
</h4>
</div>
</div>
</div>
</div>

如果您首先“按原样”运行以下命令,您应该会在 textarea 中看到修改后的文档的表示,然后,如果您启用第二个 URL 并运行它(没有 scheme),则会出现问题。

这与您的代码基本相同 - 如果没有看到正确的网址,很难说问题出在哪里,因为您有两个未声明和未知的方法get_page_by_titleget_rating

从下面的代码可以看出,不需要在foreach循环中调用一个新的 DOMDocument 对象来简单地添加一个新节点

<?php
    $url='https://www.php.net/manual/en/class.domdocument.php';
    #$url='php.net/manual/en/class.domdocument.php';
    
    
    libxml_use_internal_errors( true );
    $dom=new DOMDocument;
    $dom->validateOnParse=false;
    $dom->recover=true;
    $dom->strictErrorChecking=false;
    $dom->loadHTMLFile( $url );
    libxml_clear_errors();
    
    
    $xp=new DOMXPath( $dom );
    $expr='//a[@class="methodname"]';
    
    $col=$xp->query( $expr );
    if( $col->length > 0 ){
        foreach( $col as $node ){
            $linktext=$node->nodeValue;
            $p=$dom->createElement('p','------BANANA BANANA BANANA-------');
            $node->appendChild($p);
        }
        
        printf('<textarea cols=100 rows=20>%s</textarea>',$dom->saveHTML() );
    }
?>

暂无
暂无

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

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