infowire Posted March 2, 2010 Share Posted March 2, 2010 Hello, I have a function that turns any link within a variable into an HTML clickable link but the link looks like a full link, what i would like this function to do is to take the name of the domain without the HTTP and www just the site name and make the Link be that name. So instead of my links looking like this: http://www.somesite.com i want them to look like this "somesite" and that would be a clickable link. Or a better idea would be somehow grab the links page title and use that for the <a href = "urlhere"> Page Title/Site Title </> function make_clickable($text) { $ret = ' ' . $text; $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $ret); $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); $ret = substr($ret, 1); return($ret); } Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/ Share on other sites More sharing options...
Dennis1986 Posted March 2, 2010 Share Posted March 2, 2010 $host = parse_url($url, PHP_URL_HOST); Simple as that Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/#findComment-1020649 Share on other sites More sharing options...
infowire Posted March 3, 2010 Author Share Posted March 3, 2010 How can i implement that into my code i have ? Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/#findComment-1020711 Share on other sites More sharing options...
infowire Posted March 3, 2010 Author Share Posted March 3, 2010 Anyone??? BUMP* Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/#findComment-1020999 Share on other sites More sharing options...
thebadbad Posted March 3, 2010 Share Posted March 3, 2010 Quick example: function _callback($matches) { return '<a href="' . $matches[0] . '">' . parse_url($matches[0], PHP_URL_HOST) . '</a>'; } $ret = preg_replace_callback('~\bhttp://\S+(?![^<]*?>)~i', '_callback', $ret); Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/#findComment-1021133 Share on other sites More sharing options...
thebadbad Posted March 3, 2010 Share Posted March 3, 2010 Or if you want to use the external page's title (if applicable): function _callback($matches) { $html = file_get_contents($matches[0]); if ($html != false) { if (preg_match('~<title\b[^>]*>(.+?)</title>~is', $html, $match)) { return '<a href="' . $matches[0] . '">' . $match[1] . '</a>'; } } return '<a href="' . $matches[0] . '">' . parse_url($matches[0], PHP_URL_HOST) . '</a>'; } $ret = preg_replace_callback('~\bhttp://\S+(?![^<]*?>)~i', '_callback', $ret); Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/#findComment-1021146 Share on other sites More sharing options...
infowire Posted March 4, 2010 Author Share Posted March 4, 2010 that works great, only for some reason the urls look like this : http://www.abt.com/product/40536/Friedrich-P12A.html%3C/a%3E it adds the </a> at the end of the url. EDIT I figured it out, i had to remove the </a> from my code. Quote Link to comment https://forums.phpfreaks.com/topic/193942-php-expressions-help/#findComment-1021282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.