yarri/link-finder

将文本(纯文本或HTML)中的不可点击的URL和电子邮件地址转换为可点击的HTML链接

v2.7.11 2023-12-06 09:25 UTC

README

Build Status Downloads Codacy Badge

在纯文本文档中,LinkFinder 搜索URL和电子邮件地址并使它们可点击,在HTML文档中搜索缺失的链接并将它们转换为可点击链接。

用法

$text = '
  Welcome at www.example.com!
  Contact us on info@example.com.
';

$lf = new LinkFinder();
echo $lf->process($text);

// Welcome at <a href="https://www.example.com/">www.example.com</a>!
// Contact us on <a href="mailto:info@example.com">info@example.com</a>.

可以在选项中指定对 <a><a href="mailto:..."> 元素的额外属性

$lf = new LinkFinder([
  "attrs" => ["class" => "external-link", "target" => "_blank", "rel" => "nofollow"],
  "mailto_attrs" => ["class" => "external-email"]
]);
echo $lf->process($text);

// Welcome at <a class="external-link" href="https://www.example.com/" target="_blank" rel="nofollow">www.example.com</a>!
// Contact us on <a class="external-email" href="mailto:info@example.com">info@example.com</a>.

默认启用HTML实体的转义

$text = '
  Find more at
  <http://www.ourstore.com/>
';

$lf = new LinkFinder();
echo $lf->process($text);

// Find more at
// &lt;<a href="http://www.ourstore.com/">http://www.ourstore.com/</a>&gt;

在HTML文档中创建缺失的链接或电子邮件中的链接

$html_document = '
  <p>
    Visit <a href="http://www.ckrumlov.info/">Cesky Krumlov</a> or Prague.eu.
  </p>
';

$lf = new LinkFinder();
echo $lf->processHtml($html_document);

// <p>
//   Visit <a href="http://www.ckrumlov.info/">Cesky Krumlov</a> or <a href="https://Prague.eu">Prague.eu</a>.
// </p>

方法 $lf->processHtml() 实际上是 $lf->process($html_document,["escape_html_entities" => false]) 的别名。

在处理HTML文本的情况下,LinkFinder默认不会在标题(<h1><h2>、...)中创建链接。可以通过选项 avoid_headlines 来覆盖。

echo $lf->processHtml($html_document,["avoid_headlines" => false]);

// or

$lf = new LinkFinder(["avoid_headlines" => false]);
echo $lf->processHtml($html_document);

如果未来链接中没有指定协议(例如,www.example.com),LinkFinder应该优先选择https还是http?可以通过选项 prefer_https 来设置。默认值为true。还有一个常量 LINK_FINDER_PREFER_HTTPS 用于在全局范围内更改默认行为。

如果 prefer_https 设置为false,可以在选项 secured_websites 中指定受保护的网站列表

$lf = new LinkFinder([
  "prefer_https" => false,
  "secured_websites" => [
    "example.com",
    "webmail.example.com"
  ]
]);
echo $lf->process('Please, sign in at example.com/login/ or webmail.example.com');

// Please, sign in at <a href="https://example.com/login/">example.com/login/</a> or <a href="https://webmail.example.com">webmail.example.com</a>

如果省略了 secured_websites 选项并且启用了https协议,将自动添加当前HTTP主机($_SERVER["HTTP_HOST"])。

长URL缩短

长URL自动缩短到最多70个字符。例如,以下URL

https://venturebeat.com/2018/05/01/donkey-kong-country-tropical-freeze-review-a-funky-fresh-switch-update/

将被转换为

<a href="https://venturebeat.com/2018/05/01/donkey-kong-country-tropical-freeze-review-a-funky-fresh-switch-update/">https://venturebeat.com/2018/05/01/donkey-kong-country-tropica...</a>

如果缩短不是期望的行为,应将选项 shorten_long_urls 设置为false

$lf = new LinkFinder(["shorten_long_urls" => false]);

安装

只需使用Composer

composer require yarri/link-finder

测试

LinkFinder使用Travis CI在PHP 5.6到PHP 8.3上自动进行测试。

对于测试执行,使用了包 atk14/tester。它只是phpunit/phpunit的包装脚本。

安装开发所需依赖

composer update --dev

运行测试

cd test
../vendor/bin/run_unit_tests

许可证

LinkFinder是免费软件,根据MIT许可证条款分发