smeghead / text-link-encoder
一个库,可以将包含URL(例如用户输入的字符串)的任何字符串转换为包含URL链接的HTML字符串。非URL部分将被HTML编码。
v0.2.1
2023-11-06 13:42 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
README
一个库,可以将包含URL和电子邮件(例如用户输入的字符串)的任何字符串转换为包含URL链接的HTML字符串。非URL部分将被HTML编码。
特性
- 将文本中的URL和电子邮件转换为链接标签。
- 其他部分将被正确编码。
安装
通过Composer
$ composer require smeghead/text-link-encoder
使用
<?php require_once(__DIR__ . '/../vendor/autoload.php'); use Smeghead\TextLinkEncoder\TextLinkEncoder; use Smeghead\TextLinkEncoder\Config\TextLinkEncoderSettings; $encoder = new TextLinkEncoder(new TextLinkEncoderSettings()); echo $encoder->encode('Web Site: http://www.example.com/'); // -> Web Site: <a href="http://www.example.com/" target="_blank" rel="noreferrer noopener">http://www.example.com/</a> echo $encoder->encode('Email: info@example.com'); // -> Email: <a href="mailto:info@example.com" target="_blank" rel="noreferrer noopener">info@example.com</a> echo $encoder->encode('<script>alert(1);</script> http://www.example.com/'); // -> <script>alert(1);</script> <a href="http://www.example.com/" target="_blank" rel="noreferrer noopener">http://www.example.com/</a>
设置
<?php require_once(__DIR__ . '/../vendor/autoload.php'); use Smeghead\TextLinkEncoder\TextLinkEncoder; use Smeghead\TextLinkEncoder\Config\TextLinkEncoderSettings; $encoder = new TextLinkEncoder( (new TextLinkEncoderSettings()) ->linkTarget('_self') ->convertNewLineToBrTag(false) ); echo $encoder->encode("Web Site: http://www.example.com/\nDescription: ..."); // -> Web Site: <a href="http://www.example.com/" target="_self" rel="noreferrer noopener">http://www.example.com/</a> // Description: ...
开发
打开shell
docker compose build docker compose run php_cli bash
安装依赖项
composer install
执行测试
composer test
类图
CONTRIBUTING
问题和Pull Requests都受欢迎!