简体   繁体   中英

How can i get the text from a child node with php DOMDocument

I've been writing a php code to get information from a site, so far i was able to get the href attribute, but i cant find a way to get the text from the child node "span", can someone help me?

html- >

<a class="js-publication" href="publication/247931167"> 
    <span class="publication-title">An approach for textual authoring</span> 
</a>

This is how i am currently able to get the href ->

    @$dom->loadHTMLFile($curPage);
    $anchors = $dom->getElementsByTagName('a'); 
    foreach ($anchors as $element) {            
        $class_ = $element->getAttribute('class');
        if (0 !== strpos($class_, 'js-publication')) {
            $href = $element->getAttribute('href');
            if(0 === stripos($href,'publication/')){
                echo $href;//link para a publicação;
                echo "\n";
            }
        }
    }

You can use DOMXpath

$html = <<< LOL
<a class="js-publication" href="publication/247931167"> 
    <span class="publication-title">An approach for textual authoring</span> 
</a>
LOL;

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//a[@class='js-publication']") as $element){
    echo $element->getAttribute('href');
    echo $element->textContent;
}
//publication/247931167
//An approach for textual authoring

Or without the for loop, if you just want one element :

echo $xpath->query("//a[@class='js-publication']/span")[0]->textContent;
echo $xpath->query("//a[@class='js-publication']")[0]->getAttribute('href');

Ideone Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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