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

This package is auto-updated.

Last update: 2024-12-06 16:38:15 UTC


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.

Testing Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

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/');
// -> &lt;script&gt;alert(1);&lt;/script&gt; <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

Class Diagram

CONTRIBUTING

Both Issues and Pull Requests are welcome!