zegnat/linkextractor

一个尝试确定HTML文档链接到哪些资源的包。

0.1.0 2017-11-14 15:31 UTC

This package is not auto-updated.

Last update: 2024-09-20 02:34:27 UTC


README

LinkExtractor尝试确定HTML文档链接到哪些资源。

这试图解决2个问题

  1. 什么是HTML文档和另一个资源之间的链接,以及
  2. 如何以有用的(已解析)格式提供它们。

该类能够根据HTML5规范允许URL定义的位置从预解析的DOM中获取(并解析)每个URL。HTML5规范遵循GitHub讨论关于链接是什么的问题,并在2017年柏林IWC的个人讨论中进一步讨论。

使用版本 < 1.0

当前函数原型与1.0.0中的原型相同。这意味着您现在可以直接在代码中编写方法调用,并在稍后更新到1.0.0而不会破坏任何调用。

function extract(): array
function linksTo(string $url): bool

然而,如果您依赖于这些方法的具体输出或异常,版本1.0.0之前可能会有破坏性更改。请查看1.0.0版本路线图以了解可能还会进行哪些更改。

如果您需要这个库,不要等待1.0.0!相反,开始使用它,并留下至关重要的反馈。

安装

通过Composer

$ composer require zegnat/linkextractor

使用

// Parse an HTML file into a DOMDocument somehow, e.g.:
$dom = new \Zegnat\Html\DOMDocument();
$dom->loadHTML(file_get_contents('http://example.com/index.html'));
// Now initiate the extractor:
$extractor = new \Zegnat\LinkExtractor\LinkExtractor($dom, 'http://example.com/index.html');
var_dump(
    $extractor->linksTo('https://github.com/'),
    $extractor->linksTo('http://www.iana.org/domains/example')
);
/*
bool(false)
bool(true)
*/

许可证

BSD Zero Clause License(0BSD)。有关更多信息,请参阅LICENSE文件。