I think I solved it...
<?php
function createURL( $aUrl )
{
$url = $aUrl[1];
if( substr( $url, -1 ) == '/' )
$url = substr( $url, 0, -1 );
if( strlen( $url ) > 50 )
{
$array_url = parse_url( $url );
if( substr_count( $array_url['path'], '/' ) > 1 )
{
$array_url['file'] = substr( $array_url['path'], strrpos( $array_url['path'], '/' ) + 1 );
$array_url['path'] = substr( $array_url['path'], 0, strrpos( $array_url['path'], '/' ) + 1 );
$new_url = str_replace( $array_url['path'], '/.../', $url );
}
else
$new_url = str_replace( $array_url['path'], substr( $array_url['path'], 0, ( strlen( $array_url['path'] ) >= 6 ) ? 6 : 1 ) . '...', $url );
}
return '<a href="' . $url . '">' . $new_url . '</a>';
}
# Test it...
$foo = '[url]http://ehsanakhgari.org/mozilla/downloadday/stats/pledge-ranking-by-country-population.php?sort=asc&order=Rank[/url]<br />
[url]http://mbl.is/mm/sport/formula/2008/06/18/mercedes_minnist_fyrsta_sigursins_med_aldargomlum_b/[/url]<br />
[url]http://www.somethingawful.is/very-long-****ing-filename-which-is-so-cool-je.html#foo[/url]<br />
[url]http://members.aol.com/rynocub/this.is.a.very.long.url/this.very.long.url.was.created.by/the.rynocub.home.page/which.was.created.by/ben.tolsky/and.radioactive.chicken/which.is.a.ben.tolsky.company/this.url.is.the.longest.url.made.by/ben.tolsky/finally.this.is.over.htm[/url]<br />
[url]http://verylongurl.com/news-technology-slashdot-geek-tech-daily-web-blog-web2.0.html[/url]<br />
[url]http://verylongurl.com/programming-news-web-reddit-social-blog-web2.0-lisp-tutorial.html[/url]<br />
[url]http://verylongurl.com/finance-money-web2.0-wesabe-api-banking-financial-rest-budget.html[/url]<br />';
$string = preg_replace_callback( '#\[url\](.+?)\[/url\]#i', 'createURL', $foo );
echo $string;
?>
Works like a charm.