Skip to content

Instantly share code, notes, and snippets.

@cafuego
Created November 2, 2012 03:08
Show Gist options
  • Save cafuego/3998475 to your computer and use it in GitHub Desktop.
Save cafuego/3998475 to your computer and use it in GitHub Desktop.
urlamafication
<?php
$text = "Whatever stuff with URLs in it.";
$protocols = array('http', 'https', 'ftp');
$protocols = implode(':(?://)?|', $protocols) . ':(?://)?';
// Prepare domain name pattern.
// The ICANN seems to be on track towards accepting more diverse top level
// domains, so this pattern has been "future-proofed" to allow for TLDs
// of length 2-64.
$domain = '(?:[A-Za-z0-9._+-]+\.)?[A-Za-z]{2,64}\b';
$ip = '(?:[0-9]{1,3}\.){3}[0-9]{1,3}';
$auth = '[a-zA-Z0-9:%_+*~#?&=.,/;-]+@';
$trail = '[a-zA-Z0-9:%_+*~#&\[\]=/;?!\.,-]*[a-zA-Z0-9:%_+*~#&\[\]=/;-]';
// Prepare pattern for optional trailing punctuation.
// Even these characters could have a valid meaning for the URL, such usage is
// rare compared to using a URL at the end of or within a sentence, so these
// trailing characters are optionally excluded.
$punctuation = '[\.,?!]*?';
// Match absolute URLs.
$url_pattern = "(?:$auth)?(?:$domain|$ip)/?(?:$trail)?";
$pattern = "`((?:$protocols)(?:$url_pattern))($punctuation)`";
$count = preg_match_all($pattern, $text, $matches);
$replace = array();
// Grab the relative node link, and rewrite it to the alias. Then
// add it to the array of replacements to perform.
for($i = 0; $i < $count; $i++) {
$replace[$matches[0][$i]] = '[URL=' . $matches[0][$i] . ']' . basename($matches[0][$i]) . '[/URL]';
}
// Replace the links in the text, return it.
return strtr($text, $replace);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment