https://github.com/asika32764/php-autolink
A library to auto convert URL to link.
https://github.com/asika32764/php-autolink
autolink php-autolink url-parser
Last synced: 10 months ago
JSON representation
A library to auto convert URL to link.
- Host: GitHub
- URL: https://github.com/asika32764/php-autolink
- Owner: asika32764
- License: mit
- Created: 2015-06-11T15:05:12.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-01-13T06:17:32.000Z (over 2 years ago)
- Last Synced: 2024-05-13T19:22:04.498Z (about 2 years ago)
- Topics: autolink, php-autolink, url-parser
- Language: PHP
- Homepage: https://packagist.org/packages/asika/autolink
- Size: 63.5 KB
- Stars: 32
- Watchers: 3
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# PHP Autolink Library

[
](https://packagist.org/packages/asika/autolink)
[](https://packagist.org/packages/asika/autolink)
A library to auto convert URLs to links.
## Table of Content
* [PHP Autolink Library](#php-autolink-library)
* [Table of Content](#table-of-content)
* [Requirement](#requirement)
* [Installation via Composer](#installation-via-composer)
* [Getting Started](#getting-started)
* [Use Autolink Object](#use-autolink-object)
* [Convert Text](#convert-text)
* [Add Attributes](#add-attributes)
* [Convert Email](#convert-email)
* [Attributes Escaping](#attributes-escaping)
* [Options](#options)
* [`textLimit`](#textlimit)
* [`autoTitle`](#autotitle)
* [`stripScheme`](#stripscheme)
* [`escape`](#escape)
* [`linkNoScheme`](#linknoscheme)
* [Scheme](#scheme)
* [Link Builder](#link-builder)
## 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.
``` json
{
"require": {
"asika/autolink": "^2.0"
}
}
```
## 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:
```php
use Asika\Autolink\Autolink;
$autolink = new Autolink();
```
Create with options.
```php
use Asika\Autolink\AutolinkOptions;
$options = new AutolinkOptions(
stripScheme: false,
textLimit: null,
autoTitle: false,
escape: true,
linkNoScheme: false
);
$schemes = ['http', 'https', 'skype', 'itunes'];
$autolink = new Autolink($options, $schemes);
```
## Convert Text
This is an example text:
``` html
This is Simple URL:
http://www.google.com.tw
This is SSL URL:
https://www.google.com.tw
This is URL with multi-level query:
http://example.com/?foo[1]=a&foo[2]=b
```
We convert all URLs.
```php
$text = $autolink->convert($text);
```
Output:
``` html
This is Simple URL:
http://www.google.com.tw
This is SSL URL:
https://www.google.com.tw
This is URL with multi-level query:
http://example.com/?foo[1]=a&foo[2]=b
```
### Add Attributes
```php
$text = $autolink->convert($text, ['class' => 'center']);
```
All link will add this attributes:
```php
This is Simple URL:
http://www.google.com.tw
This is SSL URL:
https://www.google.com.tw
```
## Convert Email
Email url has no scheme, we use anoter method to convert them, and it will add `mailto:` at begin of `href`.
```php
$text = $autolink->convertEmail($text);
```
Output
``` html
foo@example.com
```
## 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:
```php
$autolink->setEscapeHandler(fn => ...);
```
## Options
### `textLimit`
We can set this option by constructor or setter:
```php
$autolink->textLimit(50);
$text = $autolink->convert($text);
```
The link text will be:
```
http://campus.asukademy.com/learning/job/84-fin...
```
Use Your own limit handler by set a callback:
```php
$autolink->textLimit(function($url) {
return substr($url, 0, 50) . '...';
});
```
Or use `\Asika\Autolink\LinkHelper::shorten()` Pretty handler:
```php
$autolink->textLimit(function($url) {
return \Asika\Autolink\Autolink::shortenUrl($url, 15, 6);
});
```
Output:
``` text
http://campus.asukademy.com/....../84-find-interns......
```
### `autoTitle`
Use AutoTitle to force add title on anchor element.
```php
$autolink->autoTitle(true);
$text = $autolink->convert($text);
```
Output:
``` html
http://www.google.com.tw
```
### `stripScheme`
Strip Scheme on link text:
```php
$autolink->stripScheme(true);
$text = $autolink->convert($text);
```
Output
``` html
www.google.com.tw
```
### `escape`
Auto escape URL, default is `true`:
```php
$autolink->autoEscape(false);
$text = $autolink->convert($text);
$autolink->autoEscape(true);
$text = $autolink->convert($text);
```
Output
``` html
http://www.google.com.tw?foo=bar&yoo=baz
http://www.google.com.tw?foo=bar&yoo=baz
```
### `linkNoScheme`
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
``` html
www.google.com.tw
```
## 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`.
## Link Builder
If you don't want to use `` element as your link, you can set a callback to build link HTML.
```php
$autolink->setLinkBuilder(function(string $url, array $attribs) {
$attribs['src'] = htmlspecialchars($url);
return \Asika\Autolink\HtmlBuilder::create('img', $attribs, null);
});
```