I have written this function, with help from patterns I’ve found online. But I’ve ran into a problem, while it outputs the link correctly, the actual link is something like:
http://localhost:8081/myfolder/www.sitepointforums.com/grab?foo=bar
which is obviously wrong. I am trying to simply grab multiple types of urls (in various formats [strict or lazy]) and convert them to hyperlinks.
Here’s my function:
/*******************************
* Make URL's In Text <a> links
* ------------------------------------
* @params <str> $text
* <str> $link_params
* @returns <str> $text
*
* Returns the text string supplied
* with URL's converted to working <a>
* links. The optional $link_params
* parameter can be used to add "rel"
* or "class", "id", etc to the actual
* <a> link.
*/
function Make_URLS_HTML_Links($text, $link_params = false)
{
/* OLD */
/*
$regexp = '#([\
])www\\.([a-z0-9\\-]+)\\.([a-z0-9\\-.\\~]+)((?:/[^,\ \
\\r]*)?)#i';
$link_format = '\\\\1<a '.$link_params.' href="http://www.\\\\2.\\\\3\\\\4" target="_blank">www.\\\\2.\\\\3\\\\4</a>';
return preg_replace($regexp, $link_format, $text);
*/
/* NEW */
$pattern = "@\\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\\@)?(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\\@&=+$,%#-]+)*/?)@";
$link_params = ( $link_params != false && $link_params != '' ) ? ' '.$link_params : $link_params;
$link = preg_replace($pattern, '<a href="\\0"'.$link_params.'>\\0</a>', $text);
# here i am trying to find http or https, but this doesn't seem to work either
if ( preg_match("#(http|https)#", $link) == false ):
$link = 'http://'.$link;
endif;
return $link;
}