我正在编写一个脚本,该脚本必须从URL中提取所有标签,而不仅仅是从标签中提取值,我的意思是所有标签代码如下:
<a href="test">Text</a>
我发现了一些带有preg_match_all的东西,但这仅从 href、标题等中提取值,而不是整个标签代码。我应该做什么?
你可以使用HTML解析器:用PHP5+编写的HTML DOM解析器可以让你以一种非常简单的方式操作HTML!
使用 Simplehtmldom 库从 url 获取数据
// Include the library
include('simple_html_dom.php');
// Retrieve the DOM from a given URL
$html = file_get_html('http://davidwalsh.name/');
// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e)
echo $e->href . '<br>';
// Retrieve all images and print their SRCs
foreach($html->find('img') as $e)
echo $e->src . '<br>';