PHP library to parse urls from string input

Overview

Url highlight logo


Build status Packagist version PHP version License

Url highlight - PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input.

Features:

  • Replace URLs in string by HTML tags (make clickable)
  • Match URLs without scheme by top-level domain
  • Work with HTML entities encoded input
  • Extract URLs from string
  • Check if string is URL

🚀 See examples 👀

Installation

Install the latest version with Composer:

composer require vstelmakh/url-highlight

Also, there are Twig logo Twig extension and Symfony logo Symfony bundle available.

Quick start

<?php
require __DIR__ . '/vendor/autoload.php';

use VStelmakh\UrlHighlight\UrlHighlight;

$urlHighlight = new UrlHighlight();
echo $urlHighlight->highlightUrls('Hello, http://example.com.');

// Output:
// Hello, <a href="http://example.com">http://example.com</a>.

💡 Tip: For customizing highlight see Highlighter. To properly handle HTML entity escaped string see Encoder.

Usage

Check if string is URL

<?php
$urlHighlight->isUrl('http://example.com'); // return: true
$urlHighlight->isUrl('Other string'); // return: false

Parse URLs from string

<?php
$urlHighlight->getUrls('Hello, http://example.com.');
// return: ['http://example.com']

Replace URLs by HTML tags (make clickable)

<?php
$urlHighlight->highlightUrls('Hello, http://example.com.');
// return: 'Hello, <a href="http://example.com">http://example.com</a>.'

Configuration

There are 3 parts which could be configured according to your needs:

  • Validator - define if match is valid and should be recognized as URL (e.g. allow/disallow specific schemes)
  • Highlighter - define the way how URL should be highlighted (e.g. replaced by html <a> tag)
  • Encoder - define how to work with encoded input (e.g. html special chars)

Configuration provided via constructor implementing corresponding interface instance. Use null to keep default:

<?php
use VStelmakh\UrlHighlight\Encoder\HtmlSpecialcharsEncoder;
use VStelmakh\UrlHighlight\UrlHighlight;
use VStelmakh\UrlHighlight\Validator\Validator;

$validator = new Validator();
$encoder = new HtmlSpecialcharsEncoder();
$urlHighlight = new UrlHighlight($validator, null, $encoder);

Validator

There is one validator bundled with the library. Which is used by default with settings listed in example below.

🛠️ Validator usage example
<?php
use VStelmakh\UrlHighlight\UrlHighlight;
use VStelmakh\UrlHighlight\Validator\Validator;

$validator = new Validator(
    true, // bool - if should use top level domain to match urls without scheme
    [],   // string[] - array of blacklisted schemes
    [],   // string[] - array of whitelisted schemes
    true  // bool - if should match emails (if match by TLD set to "false" - will match only "mailto" urls)
);
$urlHighlight = new UrlHighlight($validator);

💡 Tip: If you need custom behavior - create and use your own validator implementing ValidatorInterface.

Highlighter

There are 2 highlighters bundled with the library:

  • HtmlHighlighter - convert matches to html tags.
    Example: http://example.com<a href="http://example.com">http://example.com</a>

  • MarkdownHighlighter - convert matches to markdown format.
    Example: http://example.com[http://example.com](http://example.com)

By default, HtmlHighlighter is used, with settings listed in example below.

🛠️ Highlighter usage example
<?php
use VStelmakh\UrlHighlight\Highlighter\HtmlHighlighter;
use VStelmakh\UrlHighlight\UrlHighlight;

$highlighter = new HtmlHighlighter(
    'http', // string - scheme to use for urls matched by top level domain
    [],     // string[] - key/value map of tag attributes, e.g. ['rel' => 'nofollow', 'class' => 'light']
    '',     // string - content to add before highlight: {here}<a...
    ''      // string - content to add after highlight: ...</a>{here}
);
$urlHighlight = new UrlHighlight(null, $highlighter);

💡 Tip: If you need custom behavior - extend HtmlHighlighter or implement HighlighterInterface.
For more details and examples see 🖍️ Custom highlighter.

Encoder

Encoder should be used to handle encoded input properly. For example HTML escaped string could contain something like: http://example.com?a=1&quot; or http://example.com?a=1&amp;b=2 which will be wrongly matched as URL.

By default, there is no encoder used. There are 2 encoders bundled with library:

  • HtmlEntitiesEncoder - to work with HTML entities encoded string (any character expected to be HTML entity encoded)
  • HtmlSpecialcharsEncoder - to work with HTML escaped string (only & " ' < > expected to be encoded)
🛠️ Encoder usage example
<?php
use VStelmakh\UrlHighlight\Encoder\HtmlSpecialcharsEncoder;
use VStelmakh\UrlHighlight\UrlHighlight;

$encoder = new HtmlSpecialcharsEncoder();
$urlHighlight = new UrlHighlight(null, null, $encoder);

$urlHighlight->highlightUrls('&lt;a href=&quot;http://example.com&quot;&gt;Example&lt;/a&gt;');
// return: '&lt;a href=&quot;<a href="http://example.com">http://example.com</a>&quot;&gt;Example&lt;/a&gt;'

💡 Tip: For custom behavior - create and use your own encoder implementing EncoderInterface.
Keep in mind - using encoder require more regex operations and could have performance impact. Better to not use encoder if you don't expect encoded string.

Credits

Volodymyr Stelmakh
Licensed under the MIT License. See LICENSE for more information.

You might also like...
Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.

cocur/slugify Converts a string into a slug. Developed by Florian Eckerstorfer in Vienna, Europe with the help of many great contributors. Features Re

A PHP library for generating universally unique identifiers (UUIDs).

ramsey/uuid A PHP library for generating and working with UUIDs. ramsey/uuid is a PHP library for generating and working with universally unique ident

ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

PHP library to detect and manipulate indentation of strings and files

indentation PHP library to detect and manipulate the indentation of files and strings Installation composer require --dev colinodell/indentation Usage

Library for free use Google Translator. With attempts connecting on failure and array support.

GoogleTranslateForFree Packagist: https://packagist.org/packages/dejurin/php-google-translate-for-free Library for free use Google Translator. With at

Text - Simple 1 Class Text Manipulation Library

Text - Simple 1 Class Text Manipulation Library Do you remember PHP's string functions? If not, just wrap you text with Text! It will save a minute on

The Hoa\Ustring library.

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds. Hoa\Ust

PCRE wrapping library that offers type-safe preg_* replacements.

composer/pcre PCRE wrapping library that offers type-safe preg_* replacements. If you are using a modern PHP version you are probably better off using

"結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件。 / "Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best PHP Chinese word segmentation module.

jieba-php "結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件,目前翻譯版本為 jieba-0.33 版本,未來再慢慢往上升級,效能也需要再改善,請有興趣的開發者一起加入開發!若想使用 Python 版本請前往 fxsjy/jieba 現在已經可以支援繁體中文!只要將字典切換為 bi

Comments
  • very useful!!

    very useful!!

    well done!! it would be fantastic if you made one to highlight phone numbers.. another troublesome detection regex especially with all the edge cases with international numbers and what not.

    it would also be nice if you made one for emails, although there are already some nice solutions out there for that

    thanks for taking the time to make this!

    opened by vesper8 3
  • Installing with composer didn't work for PHP version 7.0.33.0

    Installing with composer didn't work for PHP version 7.0.33.0

    `composer require vstelmakh/url-highlight

    [InvalidArgumentException]
    Could not find package vstelmakh/url-highlight at any version matching your PHP version 7.0.33.0

    require [--dev] [--prefer-source] [--prefer-dist] [--no-plugins] [--no-progress] [--no-update] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--] []...

    `

    opened by AverageGuy 2
  • Feature request: Domain whitelist / blacklist

    Feature request: Domain whitelist / blacklist

    Hey. Nice little library, thanks a lot!

    That would be really great to have a possibility to validate a domains (check if we are allowed to highlight a given link according to its domain like example.com).

    In my case, i need to highlight only one domain - and of course i will achieve that in some hacky way, but i think that will be useful to implement it out of the box.

    Cheers)

    opened by git-webmaster 1
Releases(v3.0.1)
Owner
Volodymyr Stelmakh
Volodymyr Stelmakh
A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

URLify for PHP A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js from the Django project. Handles symbols from

Aband*nthecar 667 Dec 20, 2022
A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Dec 28, 2022
A PHP string manipulation library with multibyte support

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Jan 3, 2023
🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.

?? Portable UTF-8 Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 474 Dec 22, 2022
🔡 Portable ASCII library - performance optimized (ascii) string functions for php.

?? Portable ASCII Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 380 Jan 6, 2023
:accept: Stringy - A PHP string manipulation library with multibyte support, performance optimized

?? Stringy A PHP string manipulation library with multibyte support. Compatible with PHP 7+ 100% compatible with the original "Stringy" library, but t

Lars Moelleken 144 Dec 12, 2022
A language detection library for PHP. Detects the language from a given text string.

language-detection Build Status Code Coverage Version Total Downloads Minimum PHP Version License This library can detect the language of a given text

Patrick Schur 738 Dec 28, 2022
A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

Max Base 4 Feb 22, 2022
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Motto: "Every business should have a detection script to detect mobile readers." About Mobile Detect is a lightweight PHP class for detecting mobile d

Şerban Ghiţă 10.2k Jan 4, 2023
ATOMASTIC 14 Mar 12, 2022