asika / autolink
自动将 URL 转换为链接锚点。
2.0.4
2024-05-24 08:38 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.0||^10.0||^11.0
- windwalker/test: ^4.0
- windwalker/utilities: ^4.0
README
一个自动将 URL 转换为链接的库。
目录
需求
- 2.x 版本要求 PHP 8.0 或更高。
- 1.x 版本支持 PHP 5.3 到 7.4
通过 Composer 安装
将此添加到 composer.json 中的 require 块。
{ "require": { "asika/autolink": "^2.0" } }
入门
这是将 URL 转换为链接的快速入门方法
use Asika\Autolink\AutolinkStatic; $text = AutolinkStatic::convert($text); $text = AutolinkStatic::convertEmail($text);
使用 Autolink 对象
创建对象
use Asika\Autolink\Autolink; $autolink = new Autolink();
使用选项创建
$options = [ 'strip_scheme' => false, 'text_limit' => false, 'auto_title' => false, 'escape' => true, 'link_no_scheme' => false ]; $schemes = ['http', 'https', 'skype', 'itunes']; $autolink = new Autolink($options, $schemes);
转换文本
这是一段示例文本
This is Simple URL: http://www.google.com.tw This is SSL URL: https://www.google.com.tw This is URL with multi-level query: http://example.com/?foo[1]=a&foo[2]=b
我们将转换所有 URL。
$text = $autolink->convert($text);
输出
This is Simple URL: <a href="http://www.google.com.tw">http://www.google.com.tw</a> This is SSL URL: <a href="https://www.google.com.tw">https://www.google.com.tw</a> This is URL with multi-level query: <a href="http://example.com/?foo[1]=a&foo[2]=b">http://example.com/?foo[1]=a&foo[2]=b</a>
添加属性
$text = $autolink->convert($text, ['class' => 'center']);
所有链接都将添加这些属性
This is Simple URL: <a href="http://www.google.com.tw" class="center">http://www.google.com.tw</a> This is SSL URL: <a href="https://www.google.com.tw" class="center">https://www.google.com.tw</a>
转换电子邮件
电子邮件 URL 没有方案,我们使用另一种方法来转换它们,并且将在 href
的开始处添加 mailto:
。
$text = $aurolink->convertEmail($text);
输出
<a href="mailto:foo@example.com">foo@example.com</a>
属性转义
由于 PHP 8.1 或更高版本的 htmlspecialchars()
默认将单引号转义,Autolink 也会在 8.0 中转义单引号。使用此方法以在任何 PHP 版本上保持相同的转义行为。
$autolink->escape('...');
如果您想更改转义行为,请设置自定义的转义处理程序
$autolink->setEscapeHandler(fn => ...);
选项
text_limit
我们可以通过构造函数或设置器设置此选项
$auitolink->textLimit(50); $text = $autolink->convert($text);
链接文本将是
http://campus.asukademy.com/learning/job/84-fin...
通过设置回调使用您自己的限制处理程序
$auitolink->textLimit(function($url) { return substr($url, 0, 50) . '...'; });
或使用 \Asika\Autolink\LinkHelper::shorten()
美化处理程序
$auitolink->textLimit(function($url) { return \Asika\Autolink\Autolink::shortenUrl($url, 15, 6); });
输出
http://campus.asukademy.com/....../84-find-interns......
auto_title
使用 AutoTitle 强制在锚点元素上添加标题。
$autolink->autoTitle(true); $text = $autolink->convert($text);
输出
<a href="http://www.google.com.tw" title="http://www.google.com.tw">http://www.google.com.tw</a>
strip_scheme
从链接文本中删除方案
$auitolink->stripScheme(true); $text = $autolink->convert($text);
输出
<a href="http://www.google.com.tw" >www.google.com.tw</a>
escape
自动转义 URL,默认为 true
$auitolink->autoEscape(false); $text = $autolink->convert($text); $auitolink->autoEscape(true); $text = $autolink->convert($text);
输出
<a href="http://www.google.com.tw?foo=bar&yoo=baz" >http://www.google.com.tw?foo=bar&yoo=baz</a> <a href="http://www.google.com.tw?foo=bar&yoo=baz" >http://www.google.com.tw?foo=bar&yoo=baz</a>
link_no_scheme
转换没有方案的 URL。如果将 TRUE
传递给此选项,Autolink 将使用 http
作为默认方案,您也可以提供自己的默认方案。
$auitolink->linkNoScheme('https'); $text = $autolink->convert('www.google.com.tw');
输出
<a href="https://www.google.com.tw" >www.google.com.tw</a>
方案
您可以为以该方案开始的 URL 添加新的方案,例如:vnc://example.com
$autolink->addScheme('skype', 'vnc');
默认方案为 http, https, ftp, ftps
。
链接构建器
如果您不想使用 <a>
元素作为您的链接,您可以设置一个回调来构建链接 HTML。
$autolink->setLinkBuilder(function(string $url, array $attribs) { $attribs['src'] = htmlspecialchars($url); return \Asika\Autolink\HtmlBuilder::create('img', $attribs, null); });