I agree with Ken2k7 that a database would be optimal, but this saves on queries to the database, and there are very few people who would rather decode a URL than just click the link. Here are the two functions I use:
function getLink($matches)
{
if (preg_match('/^www\./', $matches[2]))
$link = 'http://' . $matches[2];
else
$link = $matches[2];
$link = base64_encode($link);
$result = $matches[1] . 'link.php?url=' . $link. $matches[3];
return $result;
}
function linkMask($text)
{
$pattern = '/(<a href=["|\'])(.*?)?(["|\']>)/';
$text = preg_replace_callback($pattern, "getLink", $text);
return $text;
}
I can't guarantee it's bug free. You'll want to run whatever text you have through the linkMask() function and it will do the rest. It also fixes links incase you mistakenly type www.blah.com instead of using the http://. To use it, the links are just in normal "a href" format that it checks for.
On my link.php page, it looks like this:
$target = base64_decode($_GET['url']);
header ("Location: $target");
Mine has a lot more features, but that's the basic jist and I think you're knowledgeable to modify it to your needs.