Giter Site home page Giter Site logo

php-autolink's Introduction

PHP Autolink Library

A library to auto convert URLs to links.

Table of Content

Requirement

  • Version 2.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:

use Asika\Autolink\AutolinkStatic;

$text = AutolinkStatic::convert($text);
$text = AutolinkStatic::convertEmail($text);

Use Autolink Object

Create the object:

use Asika\Autolink\Autolink;

$autolink = new Autolink();

Create with options.

$options = [
    'strip_scheme' => false,
    'text_limit' => false,
    '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:

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.

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

Output:

This is Simple URL:
<a href="http://www.google.com.tw">http://www.google.com.tw</a>

This is SSL URL:
<a href="https://www.google.com.tw">https://www.google.com.tw</a>

This is URL with multi-level query:
<a href="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 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.

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

Output

Options

text_limit

We can set this option by constructor or setter:

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

$auitolink->textLimit(function($url) {
    return substr($url, 0, 50) . '...';
});

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

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

Output:

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

auto_title

Use AutoTitle to force add title on anchor element.

$autolink->autoTitle(true);

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

Output:

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

strip_scheme

Strip Scheme on link text:

$auitolink->stripScheme(true);

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

Output

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

escape

Auto escape URL, default is true:

$auitolink->autoEscape(false);

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

$auitolink->autoEscape(true);

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

Output

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

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.

$auitolink->linkNoScheme('https');

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

Output

<a href="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 is http, 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);
});

php-autolink's People

Contributors

arnt avatar asika32764 avatar burki avatar timothybjacobs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

php-autolink's Issues

Seebz autolink

<?php

function autolink($str, $attributes=array()) {
    $attrs = '';
    foreach ($attributes as $attribute => $value) {
        $attrs .= " {$attribute}=\"{$value}\"";
    }

    $str = ' ' . $str;
    $str = preg_replace(
        '`([^"=\'>])((http|https|ftp)://[^\s<]+[^\s<\.)])`i',
        '$1<a href="$2"'.$attrs.'>$2</a>',
        $str
    );
    $str = substr($str, 1);

    return $str;
}

?>

Includes period(.) when autolinking

Hello, if the url is at the end of a sentence, the auto-linking incorrectly includes the period(.) as part of the url which means the url fails.

For example:

My first paragraph. This is my website url https://google.com.

This is another paragraph.

In the example above the autolinking results in incorrectly hyperlinking https://google.com.

Link without scheme

A link without scheme e.g. "www.google.de" does not work. I think this should be converted as a link too. ;)

http://localhost/ isn't converted

http://localhost/ doesn't match the regular expression of the convert function.
Reason:
The regular expression expects at least one dot inside the domain name.

$this->getSchemes(true) . ")://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}([/a-zA-Z0-9-._~:?#[]@!$&'()+,;=%">])?)/"

phpStan errors

Hi there! Congrats on your 2.0.0 release! ๐ŸŽ‰

phpStan is now throwing errors

Static method Asika\Autolink\AutolinkStatic::convertEmail() invoked with 2 parameters, 0 required.

Static method Asika\Autolink\AutolinkStatic::convert() invoked with 2 parameters, 0 required.

on lines like this:

        $html = AutolinkStatic::convertEmail($html, ['class' => 'email-link']);
        $html = AutolinkStatic::convert($html, ['class' => 'external-link']);

What license is this package?

LICENSE.md indicates the repository is MIT License. But in every PHP files' header, there is a license header showing it's GPLv2-or-later:

* @license GNU General Public License version 2 or later;

If it is MIT License, can you update the header and release a new version?

Open link in a new tab?

Is it possible to extend the functionality of this package to be able to add target="_blank" ?
Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.