smeghead / text-link-encoder
A library that converts any string containing a URL (such as a user-entered string) into a html string containing url link. Non-URL parts are html-encoded.
v0.2.1
2023-11-06 13:42 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
README
A library that converts any string containing URLs and Emails (such as a user-entered string) into a html string containing url link. Non-URL parts are html-encoded.
Features
- Converts urls and Emails in text to link tags.
- Other parts are properly encoded.
Install
From Composer
$ composer require smeghead/text-link-encoder
Usage
<?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>
Settings
<?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: ...
Development
Open shell
docker compose build docker compose run php_cli bash
install dependencies
composer install
execute tests
composer test
Class Diagram
CONTRIBUTING
Both Issues and Pull Requests are welcome!