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.
- Converts urls and Emails in text to link tags.
- Other parts are properly encoded.
$ 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>
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: ...
docker compose build
docker compose run php_cli bash
composer install
composer test
Both Issues and Pull Requests are welcome!