Convert text URLs or Hyperlinks to clickable links

Here's a PHP code that can parse text URLs or Hyperlinks into a clickable link. This is useful when displaying text contents with inline links written along.

Some of the URLs this script can handle:
  • http://www.google.com
  • https://github.com/pda/pheanstalk/
  • http://www.google.com/search?client=ubuntu&channel=fs&q=search+this&ie=utf-8&oe=utf-8.php

/**
 * @param String $s
 * @return String
 */
public static function markupUrls($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@''<a href="$1" target="_blank">$1</a>'$s);
}


Related posts:

Bookmark and Share

2 comments :

Dwndr said...

can I use this for Blogspot as well?

bassgrinder said...

Not sure if you could use that in Blogspot. Perhaps look for a Javascript counterpart of this. They're more welcome inside Blogspot themes.

Post a Comment

Hi there! Please leave your message here. Also, I may not be able to respond to your query right away. So please bear with me. Thanks. :)