提取所有< a>内容标签 [英] extract all <a> tags from content

查看:98
本文介绍了提取所有< a>内容标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取某些页面内的所有链接及其链接文本以显示在一个块中.我已经尝试过正则表达式,但是由于标签没有修复模式,它们具有各种属性,因此无法得到正确的结果.我使用PHP.

I want to extract all links inside some pages with their link text to show in a block. I 've tried regex but it doesn't get correct result because the tags don't have a fix pattern,they have various attribute.I use PHP.

我该怎么办?

推荐答案

DOMDocument: :loadHTML .

喜欢:

$doc = new DOMDocument();
$doc->loadHTML($string);
foreach($doc->getElementsByTagName('a') as $a) {
    // do something with $a->textContent
} 

这篇关于提取所有< a>内容标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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