-
-
Save joshhartman/2212140 to your computer and use it in GitHub Desktop.
Revisions
-
joshhartman revised this gist
Mar 27, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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">embedded links</a> and <em>other</em> <strong>HTML tags</strong>.'; echo linkify($content); -
joshhartman revised this gist
Mar 27, 2012 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ * @param string $mode normal or all * @return string */ 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); -
jasny revised this gist
Mar 8, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 => $val) { $attr = ' ' . $key . '="' . htmlentities($val) . '"'; } $links = array(); -
jasny revised this gist
Mar 8, 2012 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, ftp, mail, twitter * @param array $attributes * @param string $mode normal or all * @return string */ 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 ((array)$protocols as $protocol) { switch ($protocol) { 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; } -
jasny created this gist
Mar 8, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }