php正则表达式获取具体url [英] php regular expression to get the specific url

查看:47
本文介绍了php正则表达式获取具体url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下标签中获取以../category/"开头的网页的网址:

I would like to get the urls from a webpage that starts with "../category/" from these tags below:

<a href="../category/product/pc.html" target="_blank">PC</a><br>
<a href="../category/product/carpet.html" target="_blank">Carpet</a><br>

非常感谢任何建议.

谢谢!

推荐答案

不需要正则表达式.使用 DOM 进行简单的 XPath 查询就足够了:

No regular expressions is required. A simple XPath query with DOM will suffice:

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$nodes = $xpath->query('//a[starts-with(@href, "../category/")]');
foreach ($nodes as $node) {
    echo $node->nodeValue.' = '.$node->getAttribute('href').PHP_EOL;
}

将打印:

PC = ../category/product/pc.html
Carpet = ../category/product/carpet.html

这篇关于php正则表达式获取具体url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆