简体   繁体   中英

Get link from html by php

How do I get this link <li><a rel="prev" href="/1149/" accesskey="p">&lt; Prev</a></li> <li><a rel="prev" href="/1149/" accesskey="p">&lt; Prev</a></li> from an html document using PHP? How do I get the link by the "rel"? I'm trying to get /1149/

Trying to understand what you want… If you want to take an HTML/XML input and grab the href value of a link with the attribute rel="prev" I'd suggest using DOMXpath, something like:

$html = '<li><a rel="prev" href="/1149/" accesskey="p">&lt; Prev</a></li>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

foreach ($xpath->query("//a[@rel='prev']") as $node) {
    if ($node->hasAttribute('href')) {
        echo $node->getAttribute('href') . '<br>';
    }
}

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