2017-25 © Pedro Peláez
 

library autolink

Auto convert url to link anchor.

image

asika/autolink

Auto convert url to link anchor.

  • Tuesday, October 10, 2017
  • by asika32764
  • Repository
  • 1 Watchers
  • 16 Stars
  • 26,991 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 3 Open issues
  • 6 Versions
  • 60 % Grown

The README.md

PHP Autolink Library

GitHub Actions Workflow Status Packagist Version Packagist Downloads, (*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);

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)

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);
});

The Versions

10/10 2017

dev-master

9999999-dev https://github.com/asika32764/php-autolink

Auto convert url to link anchor.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Simon Asika

10/10 2017

1.0.4

1.0.4.0 https://github.com/asika32764/php-autolink

Auto convert url to link anchor.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Simon Asika

28/06 2016

1.0.3

1.0.3.0 https://github.com/asika32764/php-autolink

Auto convert url to link anchor.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Simon Asika

09/04 2016

1.0.2

1.0.2.0 https://github.com/asika32764/php-autolink

Auto convert url to link anchor.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Simon Asika

21/06 2015

1.0.1

1.0.1.0 https://github.com/asika32764/php-autolink

Auto convert url to link anchor.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Simon Asika

13/06 2015

1.0.0

1.0.0.0 https://github.com/asika32764/php-autolink

Auto convert url to link anchor.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Simon Asika