Page 1 of 1

[solved] Convert URL string to html link and shorten

Posted: Sat Dec 20, 2008 5:06 am
by rich86
Hi,
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;
    }
which does half the job ie. converts a string with a url in to a html link but i don't know how to then shorten the link.

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>
 
the link is shortened to 10 characters.

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

[solved]Re: Convert URL string to html link and shorten

Posted: Sat Dec 20, 2008 10:42 am
by rich86
Nevermind i have been playing today and sorted it using this:

Code: Select all

 
function hyperlink($text){
    // match protocol://address/path/
    $text = preg_replace_callback("#(^| |\n)([a-zA-Z]+://)([.]?[a-zA-Z0-9_/-])*#", create_function('$part',
                            '$text_link = (strlen($part[0]) > 19) ? substr($part[0],0,14) . "..." . substr($part[0],-2) : $part[0];
                            return $part[1] . "<a href=\"{$part[0]}\" title=\"Click to visit: {$part[0]}\" target=\"_blank\">{$text_link}</a>";')
                            , $text);
 
    // match http://www.something
    //added |\n so catches new lines as well and not links in the [url=...] tags or in quotes
    $text = preg_replace_callback("#(^| |\n)(www([.]?[a-zA-Z0-9_/-])*)#", create_function('$part',
                            '$text_link = (strlen($part[2]) > 15) ? substr($part[2],0,10) . "..." . substr($part[2],-2) : $part[2];
                            return $part[1] . "<a href=\"http://{$part[2]}\" title=\"Click to visit: http://{$part[2]}\" target=\"_blank\">{$text_link}</a>";')
                            , $text);
 
    // return $text
    return $text;
    }
 
using the preg_replace_callback function i was able to call a function with the expression matching and then rephrase the output string.