Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Forked from jasny/linkify.php
Created March 27, 2012 03:06
Show Gist options
  • Save joshhartman/2212140 to your computer and use it in GitHub Desktop.
Save joshhartman/2212140 to your computer and use it in GitHub Desktop.

Revisions

  1. joshhartman revised this gist Mar 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion linkify.php
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,6 @@ function linkify($value, $protocols = array('http', 'mail'), array $attributes =

    // Example:

    $content = 'You can use this function for linking text such as http://amazon.com or www.bn.com, but it also preserves <a href="http://www.example.com">embeded links</a> and <em>other</em> <strong>HTML tags</strong>.';
    $content = 'You can use this function for linking text such as http://amazon.com or www.bn.com, but it also preserves <a href="http://www.example.com">embedded links</a> and <em>other</em> <strong>HTML tags</strong>.';

    echo linkify($content);
  2. joshhartman revised this gist Mar 27, 2012. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion linkify.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    * @param string $mode normal or all
    * @return string
    */
    public function linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal')
    function linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal')
    {
    // Link attributes
    $attr = '';
    @@ -35,3 +35,9 @@ public function linkify($value, $protocols = array('http', 'mail'), array $attri
    // Insert all link
    return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value);
    }

    // Example:

    $content = 'You can use this function for linking text such as http://amazon.com or www.bn.com, but it also preserves <a href="http://www.example.com">embeded links</a> and <em>other</em> <strong>HTML tags</strong>.';

    echo linkify($content);
  3. @jasny jasny revised this gist Mar 8, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions linkify.php
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ public function linkify($value, $protocols = array('http', 'mail'), array $attri
    {
    // Link attributes
    $attr = '';
    foreach ($attributes as $key => $value) {
    $attr = ' ' . $key . '="' . htmlentities($value) . '"';
    foreach ($attributes as $key => $val) {
    $attr = ' ' . $key . '="' . htmlentities($val) . '"';
    }

    $links = array();
  4. @jasny jasny revised this gist Mar 8, 2012. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions linkify.php
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    * Turn all URLs in clickable links.
    *
    * @param string $value
    * @param array $protocols http, https, mailto, twitter, ftp
    * @param array $protocols http/https, ftp, mail, twitter
    * @param array $attributes
    * @param string $mode normal or all
    * @return string
    */
    public function linkify($value, $protocols = array('http', 'https', 'mailto'), array $attributes = array(), $mode = 'normal')
    public function linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal')
    {
    // Link attributes
    $attr = '';
    @@ -22,11 +22,11 @@ public function linkify($value, $protocols = array('http', 'https', 'mailto'), a
    $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value);

    // Extract text links for each protocol
    foreach ($protocols as $protocol) {
    foreach ((array)$protocols as $protocol) {
    switch ($protocol) {
    case 'http': $value = preg_replace_callback($mode != 'all' ? '~(?:http://([^\s<]+?)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use (&$links, $attr) { $link = $match[1] ?: $match[2]; return '<' . array_push($links, '<a' . $attr . ' href="http://' . $link . '">' . $link . '</a>') . '>'; }, $value); break;
    case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:https://([^\s<]+?)|(www\.[^\s<]+?\.[^\s<]+?))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use (&$links, $attr) { $link = $match[1] ?: $match[2]; return '<' . array_push($links, '<a' . $attr . ' href="https://' . $link . '">' . $link . '</a>') . '>'; }, $value); break;
    case 'mailto': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
    case 'http':
    case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { if ($match[1]) $protocol = $match[1]; $link = $match[2] ?: $match[3]; return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">' . $link . '</a>') . '>'; }, $value); break;
    case 'mail': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
    case 'twitter': $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="https://twitter.com/' . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . '">' . $match[0] . '</a>') . '>'; }, $value); break;
    default: $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i' : '~([^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
    }
  5. @jasny jasny created this gist Mar 8, 2012.
    37 changes: 37 additions & 0 deletions linkify.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?php
    /**
    * Turn all URLs in clickable links.
    *
    * @param string $value
    * @param array $protocols http, https, mailto, twitter, ftp
    * @param array $attributes
    * @param string $mode normal or all
    * @return string
    */
    public function linkify($value, $protocols = array('http', 'https', 'mailto'), array $attributes = array(), $mode = 'normal')
    {
    // Link attributes
    $attr = '';
    foreach ($attributes as $key => $value) {
    $attr = ' ' . $key . '="' . htmlentities($value) . '"';
    }

    $links = array();

    // Extract existing links and tags
    $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value);

    // Extract text links for each protocol
    foreach ($protocols as $protocol) {
    switch ($protocol) {
    case 'http': $value = preg_replace_callback($mode != 'all' ? '~(?:http://([^\s<]+?)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use (&$links, $attr) { $link = $match[1] ?: $match[2]; return '<' . array_push($links, '<a' . $attr . ' href="http://' . $link . '">' . $link . '</a>') . '>'; }, $value); break;
    case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:https://([^\s<]+?)|(www\.[^\s<]+?\.[^\s<]+?))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use (&$links, $attr) { $link = $match[1] ?: $match[2]; return '<' . array_push($links, '<a' . $attr . ' href="https://' . $link . '">' . $link . '</a>') . '>'; }, $value); break;
    case 'mailto': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
    case 'twitter': $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="https://twitter.com/' . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . '">' . $match[0] . '</a>') . '>'; }, $value); break;
    default: $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i' : '~([^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
    }
    }

    // Insert all link
    return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value);
    }