php - 如何从页面(php)获取所有网址

我有一个带有 url 的页面,其中的描述一个接一个地列出(类似于书签/网站列表)。我如何使用 php 从该页面获取所有 url 并将它们写入 txt 文件(每行一个,只有没有描述的 url)?

页面看起来像这样:

Some description

Other description

Another one

我希望脚本的 txt 输出看起来像这样:

http://link.com

http://link2.com

http://link3.com

最佳答案

单向

$url="http://wwww.somewhere.com";
$data=file_get_contents($url);
$data = strip_tags($data,"<a>");
$d = preg_split("/<\/a>/",$data);
foreach ( $d as $k=>$u ){
    if( strpos($u, "<a href=") !== FALSE ){
        $u = preg_replace("/.*<a\s+href=\"/sm","",$u);
        $u = preg_replace("/\".*/","",$u);
        print $u."\n";
    }
}

https://stackoverflow.com/questions/1128774/

相关文章:

python - 有效地比较python中的两组

c# - 确认信息

flutter - 收到此错误 : java.net.BindException:地址已在使用中:无

ruby-on-rails - 手动安装 Xcode 命令行工具不起作用

arrays - 确定一个值是否在排序数组中的 O 时间是多少?

php - Laravel 检查数组是否为空

php - 如何在php中检查文件是否为视频类型?

git - 致命的 : failed to read object Inter

memory - 使用 16 位整数的重要性

performance - 给 Perl 的 print 一个列表或一个连接的字符串是否更快?