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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.