I created this function so that I could have a way of preventing very long non-breaking text links from breaking my web page layout. Paritcularly, this is used in my tweet / facebook section of my homepage. Whenever I posted a link to either of them, when the link URL was too long and could not be broken, it would overflow into the next column. Yes of course you could use css to do this, however the methods which are generally used are not valid with CSS2.1, so I came up with a way that programatically parses the link and replaces the text with “[Link]“. There are some limitations to this approach. First, since I use a simple solution, there is no way to preserve some information normally in the link such as target, title etc. Second it is being used for tweet and facebook statuses, so it is limited to checking only the first encountered link. It could be expanded to include multiple links, but I have no need for this at the moment so I left it out. **Update**: I noticed that it was also filtering my @links from twitter, so I made a slight change so that it will only shorten if it finds `http://` in the url text now. Anyway here is the code:
```
/*
* Replaces all link-text with '[Link]' so that links which are very long do not break web page layouts
* Note: some information is lost, such as custom title, target etc.
* Jason Ernst- 2011
*/
function filter_long_url($content)
{
$startpos = strpos($content, "<a");
if($startpos !== false)
{
$midpos = strpos($content, ">", $startpos);
if($midpos !== false)
{
$endpos = strpos($content, "</a>", $midpos);
if($endpos !== false)
{
$url = substr($content, $midpos+1, ($endpos)-($midpos+1));
if(strpos($url, "http://")===false)
return $content;
else
{
$newcontent = substr($content, 0, $startpos);
$newcontent = $newcontent . "<a href='$url' title='View Link' target='_blank'>[Link]</a>" . substr($content, $endpos+4);
}
}
else
return $content;
}
else
return $content;
}
else
return $content;
return $newcontent;
}
```
If the function gets something with a half-completed tag, it should just return the original content since it is impossible to find the other parts of the tag correctly.