Example 1: Basic add_autolinks() usage

The following example applies automatic links on the keyword "book".

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

echo $automatic_links->add_autolinks( '<p>Buy my book.</p>',
    [
        [
            'keyword' => "book",
            'url'     => "https://example.com/"
        ]
    ]
);
Output

Buy my book.

Example 2: Catch the exceptions

Both the add_autolinks() and and the set_options() methods throw exceptions. You can catch these exceptions to receive valuable debug information.

In the example below, the url index of the automatic link is missing.

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

try{

    echo $automatic_links->add_autolinks( '<p>Buy my book.</p>',
        [
            [
                'keyword' => "book"
            ]
        ]

);

}catch(Exception $exception){

    echo 'Message: ' . $exception->getMessage();

}
Output

Message: Please enter a valid url for the automatic link.

Example 3: Define multiple automatic links

With the add_autolinks() method, you can define multiple automatic links by adding multiple arrays.

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

echo $automatic_links->add_autolinks( '<p>As they entered they saw Dorian Gray. He was seated at the piano.</p>',
    [
        [
            'keyword' => "Dorian Gray",
            'url'     => "https://example.com/dorian-gray/"
        ],
        [
            'keyword' => "piano",
            'url'     => "https://example.com/piano/"
        ]
    ]
);
Output

As they entered they saw Dorian Gray. He was seated at the piano.

Example 4: Basic set_options() usage

With the set_options() method, you can set many parameters used to modify the behavior of the class.

The example below does what follows:

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

$automatic_links->set_options(
    [
        'case_sensitive_search' => true,
        'limit'                 => 1,
    ]
);

echo $automatic_links->add_autolinks( '<p>Hallward turned pale and caught his hand. “Dorian! Dorian!”</p>',
    [
        [
            'keyword' => "Dorian",
            'url'     => "https://example.com/"
        ]
    ]
);
Output

Hallward turned pale and caught his hand. “Dorian! Dorian!”

Example 5: Customize the individual automatic links

This example uses the keyword_before option to match only the occurrences of the keyword "Dorian" preceded by the word "said ".

The priority option is also used to prioritize this automatic link over the others. Note that the automatic links composed of multiple words or requiring a keyword_before or a keyword_after should generally have a higher priority.

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

$content = '<p>Dorian Gray glanced at the picture. “No.” said Dorian Gray.</p>';

echo $automatic_links->add_autolinks( $content,
    [
        [
            'keyword'               => "Dorian",
            'url'                   => "https://example.com",
            'priority'              => 1,
            'keyword_before'        => 'said ',
        ]
    ]
);
Output

Dorian Gray glanced at the picture. “No.” said Dorian Gray.

Example 6: Protect specific tags

With the protected_tags option, you can prevent the application of the automatic links on specific tags.

In the example below, the automatic links are not allowed in the strong tag:

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

$automatic_links->set_options(
   [
        'protected_tags' => [
            'strong'
        ]
    ]
);

echo $automatic_links->add_autolinks( '<p><strong>Lord Henry</strong> shrugged his shoulders.</p>',
    [
        [
            'keyword' => "Henry",
            'url'     => "https://example.com"
        ]
    ]
);
Output

Lord Henry shrugged his shoulders.

Example 7: Apply a variable number of links based on the content length

You can generate the maximum number of automatic links based on the number of characters present in the first parameter passed to add_autolinks(). In the example below, no automatic links are generated because the provided string has less than 200 characters.

require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

$automatic_links->set_options(
    [
        'general_limit_mode' => 0,
        'general_limit_characters_per_autolink' => 200
    ]
);

echo $automatic_links->add_autolinks( '<p>Dorian half opened the door.</p>',
    [
        [
            'keyword'               => "Dorian",
            'url'                   => "https://example.com"
        ]
    ]
);
Output

Dorian half opened the door.

Example 8: Limit the number of links to the same URL

The example below limits the maximum number of automatic links that target the same URL to 1.

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

$automatic_links->set_options(
    [
        'same_url_limit' => 1
    ]
);

echo $automatic_links->add_autolinks( '<p>I am not laughing, Dorian; at least I am not laughing at you.</p>',
    [
        [
            'keyword'               => "Dorian",
            'url'                   => "https://example.com/dorian/"
        ],
        [
            'keyword'               => "you",
            'url'                   => "https://example.com/dorian/"
        ],
        [
            'keyword'               => "laughing",
            'url'                   => "https://example.com/laughing/"
        ]
    ]
);
Output

I am not laughing, Dorian; at least I am not laughing at you.

Example 9: Prevent links to the current page

The example below disables the automatic links that point to the URL specified with current_url. You can use this feature to prevent the application of links that point to the current page.

Code
require_once('inc/class-daext-automatic-links.php');
$automatic_links = new DaextAutomaticLinks();

$automatic_links->set_options(
    [
        'ignore_self_autolinks' => true,
        'current_url' => 'https://example.com/hello-world/',
    ]
);

echo $automatic_links->add_autolinks( '<p>Buy my book.</p>',
    [
        [
            'keyword'               => "book",
            'url'                   => "https://example.com/hello-world/"
        ]
    ]
);
Output

Buy my book.