Coder Social home page Coder Social logo

php-linkify's Introduction

Linkify

Build Status

Converts URLs and email addresses into clickable links. It works on both snippets of HTML (or plain text) and complete HTML pages.

There are many regex variations shared on the internet for performing this task, but few are robust. Linkify contains a large number of unit tests to counter this.

It does not cover every possible valid-yet-never-used URLs and email addresses in order to handle 'real world' usage (eg no 'gopher://'). This means, for example, that it copes better with punctuation errors.

Authors

  • Chris Wilkinson

It uses regex based on John Gruber's Improved Liberal, Accurate Regex Pattern for Matching URLs.

Installation

composer require misd/linkify

Usage

$linkify = new \Misd\Linkify\Linkify();
$text = 'This is my text containing a link to www.example.com.';

echo $linkify->process($text);

Will output:

This is my text containing a link to <a href="http://www.example.com">www.example.com</a>.

Options

Options set on the constructor will be applied to all links. Alternatively you can place the options on a method call. The latter will override the former.

$linkify = new \Misd\Linkify\Linkify(array('attr' => array('class' => 'foo')));
$text = 'This is my text containing a link to www.example.com.';

echo $linkify->process($text);

Will output:

This is my text containing a link to <a href="http://www.example.com" class="foo">www.example.com</a>.

Whereas:

$linkify = new \Misd\Linkify\Linkify(array('attr' => array('class' => 'foo')));
$text = 'This is my text containing a link to www.example.com.';

echo $linkify->process($text, array('attr' => array('class' => 'bar')));

Will output:

This is my text containing a link to <a href="http://www.example.com" class="bar">www.example.com</a>.

Available options are:

attr

An associative array of HTML attributes to add to the link. For example:

array('attr' => array('class' => 'foo', 'style' => 'font-weight: bold; color: red;')

callback

A closure to call with each url match. The closure will be called for each URL found with three parameters: the url, the caption and a boolean isEmail (if $isEmail is true, then $url is equals to $caption.

If the callback return a non-null value, this value replace the link in the resulting text. If null is returned, the usual <a href="URL">CAPTION</a> is used.

$linkify = new \Misd\Linkify\Linkify(array('callback' => function($url, $caption, $isEmail) {
    return '<b>' . $caption . '</b>';
}));
echo $linkify->process('This link will be converted to bold: www.example.com.'));

php-linkify's People

Contributors

thewilkybarkid avatar grahamcampbell avatar arthurdarcet avatar bartgloudemans avatar jarofgreen avatar kanecohen avatar edea avatar miclf avatar

Watchers

James Cloos avatar

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.