Jump to content

PHP Expressions Help!


infowire

Recommended Posts

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);
}

Link to comment
https://forums.phpfreaks.com/topic/193942-php-expressions-help/
Share on other sites

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.