• Stars
    star
    112
  • Rank 311,284 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 12 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Converts URLs and email addresses into HTML links

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.'));

More Repositories

1

phone-number-bundle

Integrates libphonenumber into your Symfony2-Symfony4 application
PHP
459
star
2

guzzle-bundle

[NOT MAINTAINED] Integrates Guzzle into your Symfony2 application
PHP
99
star
3

drupal-cambridge-theme

University of Cambridge Drupal 7 theme
JavaScript
9
star
4

drupal-cambridge-profile

University of Cambridge Drupal 7 install profiles
PHP
7
star
5

drupal-raven

Raven authentication module for Drupal
PHP
4
star
6

php-highcharts

[NOT MAINTAINED] Allows the programmatic creation of Highcharts in PHP
PHP
4
star
7

twig-markdowner

Twig extension to turn Markdown formatted text into HTML
PHP
4
star
8

drupal-cambridge-demo

Demo University of Cambridge Drupal 7 site
PHP
4
star
9

linkify-bundle

Adds Linkify to your Symfony2 application, which converts URLs and email addresses into HTML links.
PHP
3
star
10

php-collections

Collections library for PHP
PHP
3
star
11

highcharts-bundle

[NOT MAINTAINED] Integrates PHP Highcharts into your Symfony2 application, which allows the programmatic creation of Highcharts
PHP
3
star
12

drupal-feature-carousel

University of Cambridge carousel feature for Drupal 7
PHP
2
star
13

drupal-feature-link

University of Cambridge custom link feature for Drupal 7
PHP
1
star
14

raven-bundle

Adds Raven authentication to your Symfony2 application
PHP
1
star
15

drupal-lookup

Lookup details module for Drupal
PHP
1
star
16

drupal-feature-image-styles

University of Cambridge image styles feature for Drupal 7
1
star
17

drupal-feature-webform-date-format

Forces webform date fields to use British date format instead of the default American format.
1
star
18

drupal-feature-news

University of Cambridge news feature for Drupal 7
1
star
19

drupal-feature-related-links

University of Cambridge related links feature for Drupal 7
1
star
20

drupal-feature-questions-and-answers

University of Cambridge questions and answers feature for Drupal 7
JavaScript
1
star
21

project-light-bundle

Adds the University of Cambridge's house style to your Symfony2 application.
JavaScript
1
star
22

drupal-feature-teasers

University of Cambridge teasers feature for Drupal 7
PHP
1
star