GirishGupta
Programmer
I want to search a string for each occurence of a link ( and then put <a> tags around it. This is what I have:
The only problem is that sometimes URLs are too long. So, I want to be able to change the URL that is displayed. For example: (semi-psuedo code)
Any ideas?
Girish Gupta
Code:
function MakeLink($text)
{
$text = ereg_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $text);
$text = ereg_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\">\\1</a>", $text);
return $text;
}
echo MakeLink("This is an email address: email@email.com [URL unfurl="true"]http://www.microsoft.com/[/URL] fnsdf");
The only problem is that sometimes URLs are too long. So, I want to be able to change the URL that is displayed. For example: (semi-psuedo code)
Code:
$arrayvar = GetAllLinks($string); // An array
foreach($link as $arrayvar) // For each one of the links
{
if (strlen($link)>=50) // if length of url is >= 50
{
$displaylink = substr($link, 0, 47).'...'; // take away last few chars and add '...'
}
else
{
$displaylink = $link; // length < 50
}
return "<a href=\"$link\">$displaylink</a>";
}
echo $the new string with the <a> tags and dots (...) if necessary;
Any ideas?
Girish Gupta