Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A library to auto convert URL to link.

License

NotificationsYou must be signed in to change notification settings

asika32764/php-autolink

Repository files navigation

GitHub Actions Workflow StatusPackagist VersionPackagist Downloads

A library to auto convert URLs to links.

Table of Content

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.

{"require": {"asika/autolink":"^2.0"    }}

Getting Started

This is a quick start to convert URL to link:

useAsika\Autolink\AutolinkStatic;$text = AutolinkStatic::convert($text);$text = AutolinkStatic::convertEmail($text);

Use Autolink Object

Create the object:

useAsika\Autolink\Autolink;$autolink =newAutolink();

Create with options.

useAsika\Autolink\AutolinkOptions;$options =newAutolinkOptions(    stripScheme:false,    textLimit:null,    autoTitle:false,    escape:true,    linkNoScheme:false);$schemes = ['http','https','skype','itunes'];$autolink =newAutolink($options,$schemes);

Convert Text

This is an example text:

This is Simple URL:http://www.google.com.twThis is SSL URL:https://www.google.com.twThis is URL with multi-level query:http://example.com/?foo[1]=a&foo[2]=b

We convert all URLs.

$text =$autolink->convert($text);

Output:

This is Simple URL:<ahref="http://www.google.com.tw">http://www.google.com.tw</a>This is SSL URL:<ahref="https://www.google.com.tw">https://www.google.com.tw</a>This is URL with multi-level query:<ahref="http://example.com/?foo[1]=a&amp;foo[2]=b">http://example.com/?foo[1]=a&amp;foo[2]=b</a>

Add Attributes

$text =$autolink->convert($text, ['class' =>'center']);

All link will add this attributes:

This is SimpleURL:<a href="http://www.google.com.tw">http://www.google.com.tw</a>This isSSLURL:<a href="https://www.google.com.tw">https://www.google.com.tw</a>

Convert Email

Email url has no scheme, we use anoter method to convert them, and it will addmailto: at begin ofhref.

$text =$autolink->convertEmail($text);

Output

<ahref="mailto:foo@example.com">foo@example.com</a>

Attributes Escaping

Ashtmlspecialchars() 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 escapebehavior same at any PHP versions:

$autolink->escape('...');

If you want to change the escape behavior, set your custom escape handler:

$autolink->setEscapeHandler(fn => ...);

Options

textLimit

We can set this option by constructor or setter:

$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:

$autolink->textLimit(function($url) {returnsubstr($url,0,50) .'...';});

Or use\Asika\Autolink\LinkHelper::shorten() Pretty handler:

$autolink->textLimit(function($url) {return \Asika\Autolink\Autolink::shortenUrl($url,15,6);});

Output:

http://campus.asukademy.com/....../84-find-interns......

autoTitle

Use AutoTitle to force add title on anchor element.

$autolink->autoTitle(true);$text =$autolink->convert($text);

Output:

<ahref="http://www.google.com.tw"title="http://www.google.com.tw">http://www.google.com.tw</a>

stripScheme

Strip Scheme on link text:

$autolink->stripScheme(true);$text =$autolink->convert($text);

Output

<ahref="http://www.google.com.tw">www.google.com.tw</a>

escape

Auto escape URL, default istrue:

$autolink->autoEscape(false);$text =$autolink->convert($text);$autolink->autoEscape(true);$text =$autolink->convert($text);

Output

<ahref="http://www.google.com.tw?foo=bar&yoo=baz">http://www.google.com.tw?foo=bar&yoo=baz</a><ahref="http://www.google.com.tw?foo=bar&amp;yoo=baz">http://www.google.com.tw?foo=bar&amp;yoo=baz</a>

linkNoScheme

Convert URL which no scheme. If you passTRUE to this option, Autolink will usehttp as default scheme, you can also provide your own default scheme.

$autolink->linkNoScheme('https');$text =$autolink->convert('www.google.com.tw');

Output

<ahref="https://www.google.com.tw">www.google.com.tw</a>

Scheme

You can add new scheme to convert URL begin with it, for example:vnc://example.com

$autolink->addScheme('skype','vnc');

Default schemes ishttp, https, ftp, ftps.

Link Builder

If you don't want to use<a> element as your link, you can set a callback to build link HTML.

$autolink->setLinkBuilder(function(string$url,array$attribs) {$attribs['src'] =htmlspecialchars($url);return \Asika\Autolink\HtmlBuilder::create('img',$attribs,null);});

About

A library to auto convert URL to link.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors6

Languages


[8]ページ先頭

©2009-2025 Movatter.jp