简体   繁体   中英

Finding a href value with Regex

I have a document with the following code inside

<link rel="next" type="application/atom+xml" href="https://xxxxxxxx.com?page=2"/>

Is there some regex that will look for a link tag with the rel of 'next' and get me the href value? This is far beyond my regex skills.

Thanks in advance

This type parsing shouldn't really be done from regex as unexpected nature of HTML can break the regex anytime.

Consider this DOM based code for this job:

$dom = new DOMDocument; 
$dom->loadXML(
  '<link rel="next" type="application/atom+xml" href="https://xxxxxxxx.com?page=2"/>'); 
$xpath = new DOMXPath($dom); 
$nodelist = $xpath->query("//link[contains(@rel, 'next')]");
for($i=0; $i < $nodelist->length; $i++) {
    $node = $nodelist->item($i);
    echo $node->getAttribute('href') . "\n";
}

OUTPUT:

https://xxxxxxxx.com?page=2

尝试这个

href=[\'"]?([^\'" >]+)

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