PHP Classes
elePHPant
Icontem

PHP Link to HTML: Convert URLs and email addresses into HTML links

Recommend this page to a friend!

  Author Author  
Name: Nahid Bin Azhar <contact>
Classes: 13 packages by
Country: Bangladesh Bangladesh
Innovation award
Innovation award
Nominee: 4x

Winner: 2x


  Detailed description   Download Download .zip .tar.gz  
This package can convert URLs and email addresses in text into HTML links.

It can take a given text string and finds URLs and email addresses to replace them with links that would open in a Web browser.

The attributes of links like CSS styles and classes can be configured.

The replacement of the links can also be done after calling a given callback function.

Details

Linkify

This a Linkify Laravel 5 supported package. Its 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

  • Main Author : Chris Wilkinson <chris.wilkinson@admin.cam.ac.uk>
  • Laravel Support : Nahid Bin Azhar <nahid.dns@gmail.com>

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

Installation

1. Add Linkify to your dependencies:

    // composer.json

    {
       // ...
       "require": {
           // ...
           "nahid/linkify": "1.1.*"
       }
    }

2. Use Composer to download and install Linkify:

    $ php composer.phar update Nahid/linkify

3. open config/app.php and in providers array add this line

```php
Nahid\Linkify\LinkifyServiceProvider::class,
```

and in aliases array add this line too

```php
'Linkify' => Nahid\Linkify\Facades\Linkify::class,
```

4. Open terminal and goto your project and run this command

```
composer dump-autoload
```

Usage

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

  Classes of Nahid Bin Azhar  >  PHP Link to HTML  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  

 

Name: PHP Link to HTML
Base name: linkify
Description: Convert URLs and email addresses into HTML links
Version: -
PHP version: 5
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image HTML HTML generation and processing View top rated classes
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Text processing Manipulating and validating text data View top rated classes
Group folder image Parsers Programming language interpreters and format parsers View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageNahid (1 directory)

  Files folder image Files  /  src  /  Nahid  
File Role Description
Files folder imageLinkify (3 files, 1 directory)

  Files folder image Files  /  src  /  Nahid  /  Linkify  
File Role Description
Files folder imageFacades (1 file)
  Plain text file Linkify.php Class Class source
  Plain text file LinkifyInterface.php Class Class source
  Plain text file LinkifyServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Nahid  /  Linkify  /  Facades  
File Role Description
  Plain text file Linkify.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagedata (7 files)
Files folder imageMisd (1 directory)
  Accessible without login Plain text file bootstrap.php Aux. Tests bootstrap script

  Files folder image Files  /  tests  /  data  
File Role Description
  Accessible without login Plain text file callback.json Data Auxiliary data
  Accessible without login Plain text file email-options.json Data Auxiliary data
  Accessible without login Plain text file email.json Data Auxiliary data
  Accessible without login Plain text file ignore-options.json Data Auxiliary data
  Accessible without login Plain text file ignore.json Data Auxiliary data
  Accessible without login Plain text file url-options.json Data Auxiliary data
  Accessible without login Plain text file url.json Data Auxiliary data

  Files folder image Files  /  tests  /  Misd  
File Role Description
Files folder imageLinkify (1 directory)

  Files folder image Files  /  tests  /  Misd  /  Linkify  
File Role Description
Files folder imageTest (5 files)

  Files folder image Files  /  tests  /  Misd  /  Linkify  /  Test  
File Role Description
  Plain text file CallbackTest.php Class Class source
  Plain text file LinkifyTest.php Class Class source
  Plain text file ProcessEmailsTest.php Class Class source
  Plain text file ProcessTest.php Class Class source
  Plain text file ProcessUrlsTest.php Class Class source

Download Download all files: linkify.tar.gz linkify.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.