简体   繁体   English

PHP-从网址中提取标签

[英]PHP - Extract a tags from a url

I'm working on a script which have to extract all a tags from a URL but not only the value from a tag, I mean all a tag code like this: 我正在处理一个脚本,该脚本必须从URL中提取所有标签,而不仅仅是从标签中提取值,我的意思是所有这样的标签代码:

<a href="test">Text</a>

I find something with preg_match_all but this only extract the values from href, title, etc, not the entire a tag code. 我用preg_match_all找到了一些东西,但这只会从href,title等中提取值,而不是整个标记代码。 What should I make? 我应该怎么做?

Use Simplehtmldom library to get data from url 使用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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM