i am trying to convert a url in a string to html code and then shorten the url between the anchor html tags.
So far i have found this function:
Code: Select all
function hyperlink($text){
// match protocol://address/path/
$text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\" title=\"Click to visit: \\0\" target=\"_blank\">\\0</a>", $text);
// match www.something
$text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\" title=\"Click to visit: http://\\2\" target=\"_blank\">\\2</a>", $text);
// return $text
return $text;
}Example:
Code: Select all
$string = "this is some text posted and i have a link to http://www.apage.co.uk/butisreallyafairlylonglink";
$string_with_link = hyperlink($string);
echo $string_with_link;
Output: this is some text posted and i have a link to <a href="http://www.apage.co.uk/butisreallyafairlylonglink" title="Click to visit: http://www.apage.co.uk/butisreallyafairlylonglink" target="_blank">www.apage.co.uk/butisreallyafairlylonglink</a>
Now what i want is:
this is some text posted and i have a link to <a href="http://www.apage.co.uk/butisreallyafairlylonglink" title="Click to visit: http://www.apage.co.uk/butisreallyafairlylonglink" target="_blank">www.apage. ...</a>
Do i need to write a function to find all urls and then take out the part between the anchor tags and shorten it? Or is there a way of doing it all in the one ereg_replace function?
Thanks in advance.
Richard