PHP code example of vstelmakh / url-highlight

1. Go to this page and download the library: Download vstelmakh/url-highlight library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vstelmakh / url-highlight example snippets



VStelmakh\UrlHighlight\UrlHighlight;

$urlHighlight = new UrlHighlight();
echo $urlHighlight->highlight('Check the example.com website.');

// Output:
// Check the <a href="http://example.com">example.com</a> website.


use VStelmakh\UrlHighlight\Highlighter\CallbackHighlighter;
use VStelmakh\UrlHighlight\Url;

$highlighter = new CallbackHighlighter(fn (Url $url) => sprintf(
    '<a href="%s" rel="nofollow">%s</a>',
    htmlspecialchars($url->toHref()),
    htmlspecialchars($url->host),
));

echo $urlHighlight->highlight('Visit http://example.com/path?q=hello today.', $highlighter);

// Output:
// Visit <a href="http://example.com/path?q=hello" rel="nofollow">example.com</a> today.


use VStelmakh\UrlHighlight\Format;

$encoded = htmlspecialchars('<b>http://example.com?a=1&b=2</b>');
echo $urlHighlight->highlight($encoded, format: Format::HtmlEncoded);

// Output:
// &lt;b&gt;<a href="http://example.com?a=1&amp;b=2">http://example.com?a=1&amp;b=2</a>&lt;/b&gt;


$urls = $urlHighlight->find('Visit example.com/path?a=1 today.');
$url = $urls[0];

$url->full;      // 'example.com/path?a=1'
$url->host;      // 'example.com'
$url->path;      // '/path'
$url->query;     // '?a=1'
$url->toHref();  // 'http://example.com/path?a=1'
$url->isEmail(); // false