Download the PHP package vstelmakh/url-highlight without Composer
On this page you can find all versions of the php package vstelmakh/url-highlight. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package url-highlight
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:
Also, there are Twig extension and Symfony bundle available.
Quick start
💡 Tip: For customizing highlight see Highlighter. To properly handle HTML entity escaped string see Encoder.
Usage
Check if string is URL
Parse URLs from string
Replace URLs by HTML tags (make clickable)
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:
Validator
There is one validator bundled with the library. Which is used by default with settings listed in example below.
🛠️ Validator usage example
💡 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
💡 Tip: If you need custom behavior - extend 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"
or http://example.com?a=1&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
💡 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.
All versions of url-highlight with dependencies
ext-json Version *
ext-mbstring Version *
symfony/polyfill-php72 Version ^1.10