PHP Autolink Library
, (*1)
A library to auto convert URLs to links., (*2)
Table of Content
* PHP Autolink Library
* Table of Content
* Requirement
* Installation via Composer
* Getting Started
* Use Autolink Object
* Convert Text
* Add Attributes
* Convert Email
* Attributes Escaping
* Options
* text_limit
* auto_title
* strip_scheme
* escape
* link_no_scheme
* Scheme
* Link Builder
, (*3)
Requirement
- Version 2.1.x require PHP 8.2 or higher.
- Version 2.0.x require PHP 8.0 or higher.
- Version 1.x supports PHP 5.3 to 7.4
Installation via Composer
Add this to composer.json require block., (*4)
``` json
{
"require": {
"asika/autolink": "^2.0"
}
}, (*5)
## Getting Started
This is a quick start to convert URL to link:
```php
use Asika\Autolink\AutolinkStatic;
$text = AutolinkStatic::convert($text);
$text = AutolinkStatic::convertEmail($text);
Use Autolink Object
Create the object:, (*6)
use Asika\Autolink\Autolink;
$autolink = new Autolink();
Create with options., (*7)
$options = [
'strip_scheme' => false,
'text_limit' => null,
'auto_title' => false,
'escape' => true,
'link_no_scheme' => false
];
$schemes = ['http', 'https', 'skype', 'itunes'];
$autolink = new Autolink($options, $schemes);
Convert Text
This is an example text:, (*8)
``` html
This is Simple URL:
http://www.google.com.tw, (*9)
This is SSL URL:
https://www.google.com.tw, (*10)
This is URL with multi-level query:
http://example.com/?foo[1]=a&foo[2]=b, (*11)
We convert all URLs.
```php
$text = $autolink->convert($text);
Output:, (*12)
``` html
This is Simple URL:
http://www.google.com.tw, (*13)
This is SSL URL:
https://www.google.com.tw, (*14)
This is URL with multi-level query:
http://example.com/?foo[1]=a&foo[2]=b, (*15)
### Add Attributes
```php
$text = $autolink->convert($text, ['class' => 'center']);
All link will add this attributes:, (*16)
This is Simple URL:
<a href="http://www.google.com.tw" class="center">http://www.google.com.tw</a>
This is SSL URL:
<a href="https://www.google.com.tw" class="center">https://www.google.com.tw</a>
Convert Email
Email url has no scheme, we use anoter method to convert them, and it will add mailto:
at begin of href
., (*17)
$text = $autolink->convertEmail($text);
Output, (*18)
``` html
foo@example.com, (*19)
## Attributes Escaping
As `htmlspecialchars()` in PHP 8.1 or higher will escape single quote as default,
Autolink will also escape single quote even in 8.0. Use this method to keep all escape
behavior same at any PHP versions:
```php
$autolink->escape('...');
If you want to change the escape behavior, set your custom escape handler:, (*20)
$autolink->setEscapeHandler(fn => ...);
Options
text_limit
We can set this option by constructor or setter:, (*21)
$autolink->textLimit(50);
$text = $autolink->convert($text);
The link text will be:, (*22)
http://campus.asukademy.com/learning/job/84-fin...
Use Your own limit handler by set a callback:, (*23)
$autolink->textLimit(function($url) {
return substr($url, 0, 50) . '...';
});
Or use \Asika\Autolink\LinkHelper::shorten()
Pretty handler:, (*24)
$autolink->textLimit(function($url) {
return \Asika\Autolink\Autolink::shortenUrl($url, 15, 6);
});
Output:, (*25)
``` text
http://campus.asukademy.com/....../84-find-interns......, (*26)
### `auto_title`
Use AutoTitle to force add title on anchor element.
```php
$autolink->autoTitle(true);
$text = $autolink->convert($text);
Output:, (*27)
``` html
http://www.google.com.tw, (*28)
### `strip_scheme`
Strip Scheme on link text:
```php
$autolink->stripScheme(true);
$text = $autolink->convert($text);
Output, (*29)
``` html
www.google.com.tw, (*30)
### `escape`
Auto escape URL, default is `true`:
```php
$autolink->autoEscape(false);
$text = $autolink->convert($text);
$autolink->autoEscape(true);
$text = $autolink->convert($text);
Output, (*31)
``` html
http://www.google.com.tw?foo=bar&yoo=baz
http://www.google.com.tw?foo=bar&yoo=baz, (*32)
### `link_no_scheme`
Convert URL which no scheme. If you pass `TRUE` to this option, Autolink will use
`http` as default scheme, you can also provide your own default scheme.
```php
$autolink->linkNoScheme('https');
$text = $autolink->convert('www.google.com.tw');
Output, (*33)
``` html
www.google.com.tw, (*34)
## Scheme
You can add new scheme to convert URL begin with it, for example: `vnc://example.com`
```php
$autolink->addScheme('skype', 'vnc');
Default schemes is http, https, ftp, ftps
., (*35)
Link Builder
If you don't want to use <a>
element as your link, you can set a callback to build link HTML., (*36)
$autolink->setLinkBuilder(function(string $url, array $attribs) {
$attribs['src'] = htmlspecialchars($url);
return \Asika\Autolink\HtmlBuilder::create('img', $attribs, null);
});