Description
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.
Url highlight alternatives and similar libraries
Based on the "Strings" category.
Alternatively, view Url highlight alternatives based on common mentions on social networks and blogs.
-
Google Translate For Free
google-translator translation language-detection detect-language translate php google-translate translator -
Russian metaphone phonetic algorithm implementation for PHP
Russian metaphone algorithm implementation
Get performance insights in less than 4 minutes
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest. Visit our partner's website for more details.
Do you think we are missing an alternative of Url highlight or a related project?
README
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 ๐](./docs/examples.md)
Installation
Install the latest version with Composer:
composer require vstelmakh/url-highlight
Also, there are Twig extension and 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](./src/Validator/ValidatorInterface.php).
Highlighter
There are 2 highlighters bundled with the library:
[HtmlHighlighter](./src/Highlighter/HtmlHighlighter.php) - convert matches to html tags.
Example:http://example.com
→<a href="http://example.com">http://example.com</a>
[MarkdownHighlighter](./src/Highlighter/MarkdownHighlighter.php) - 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](./src/Highlighter/HtmlHighlighter.php) or implement [HighlighterInterface](./src/Highlighter/HighlighterInterface.php).
For more details and examples see [๐๏ธ Custom highlighter](./docs/highlighter-custom.md).
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](./src/Encoder/HtmlEntitiesEncoder.php) - to work with HTML entities encoded string (any character expected to be HTML entity encoded)
- [HtmlSpecialcharsEncoder](./src/Encoder/HtmlSpecialcharsEncoder.php) - 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('<a href="http://example.com">Example</a>');
// return: '<a href="<a href="http://example.com">http://example.com</a>">Example</a>'
๐ก Tip: For custom behavior - create and use your own encoder implementing [EncoderInterface](./src/Encoder/EncoderInterface.php).
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](LICENSE) for more information.
*Note that all licence references and agreements mentioned in the Url highlight README section above
are relevant to that project's source code only.