PHP code example of hirasso / autolinker

1. Go to this page and download the library: Download hirasso/autolinker 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/ */

    

hirasso / autolinker example snippets


use Hirasso\Autolinker\Autolinker;

echo Autolinker::link('Visit https://example.com or mail [email protected]');
// Visit <a href="https://example.com">example.com</a>
// or mail <a href="mailto:[email protected]">[email protected]</a>

echo Autolinker::link(
    '<!doctype html><html><head><title>t</title></head>'
    . '<body class="x">Visit https://example.com</body></html>'
);
// <!doctype html><html><head><title>t</title></head>
// <body class="x">Visit <a href="https://example.com">example.com</a></body></html>

echo Autolinker::link('See https://example.com.');
// See <a href="https://example.com">example.com</a>.

use Dom\HTMLElement;
use Hirasso\Autolinker\Autolinker;
use Hirasso\Autolinker\AutolinkerOptions;

echo Autolinker::link($html, new AutolinkerOptions(
    urls: true,          // link urls (default: true)
    emails: true,        // link emails (default: true)
    stripScheme: true,   // strip the scheme from link texts (default: true)
    truncateText: 50,    // truncate link texts, ellipsis 

echo Autolinker::link('https://example.com/path');
// <a href="https://example.com/path">example.com/path</a>

echo Autolinker::link('https://example.com/path', new AutolinkerOptions(stripScheme: false));
// <a href="https://example.com/path">https://example.com/path</a>

use Dom\HTMLDocument;
use Hirasso\Autolinker\Autolinker;

$doc = HTMLDocument::createFromString($html, LIBXML_NOERROR);
Autolinker::link($doc); // $doc is now mutated in place