Kei*_*gan 2 php extract href hyperlink
这甚至可能......
假设我有一些带有"点击"类链接的文字:
<p>I am some text, i am some text, i am some text, i am some text
<a class="click" href="http://www.google.com">I am a link</a>
i am some text, i am some text, i am some text, i am some text</p>
Run Code Online (Sandbox Code Playgroud)
使用PHP,获取类名"click"的链接,然后获取href值?
有几种方法可以做到这一点,最快的方法是使用XPath:
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodeList = $xpath->query('//a[@class="click"]');
foreach ($nodeList as $node) {
$href = $node->getAttribute('href');
$text = $node->textContent;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4652 次 |
| 最近记录: |