Welcome to another PHP tutorial from ForgedEuphoria. This time around we’ll be looking at how to use PHP to input a TinyURL and display the real page’s URL, thus ‘uncloaking’ the link. Let’s get into it!
Here’s the full code for our function:
<?php function reverseTinyURL($url){ $url = explode('.com/', $url); $url = 'http://preview.tinyurl.com/'.$url[1]; $preview = file_get_contents($url); preg_match('/redirecturl" href="(.*)">/', $preview, $matches); return $matches[1]; } ?>
Download this code: revTinyURL.phps
Let’s break it down..
The first line of code declares that it is a function, ‘reverseTinyURL’.
function reverseTinyURL($url){
Next our function splits the URL, starting with .com. Next it gets the source of TinyURL preview page, which is where we will find our uncloaked URL.
$url = explode('.com/', $url);
$url = 'http://preview.tinyurl.com/'.$url[1];
$preview = file_get_contents($url);
Finally it uses the PHP preg_match function to find the line of HTML where our link is hidden and then returns it as a URL.
preg_match('/redirecturl" href="(.*)">/', $preview, $matches);
return $matches[1];
}
If you want to know how to use the function, see below.
<? reverseTinyURL('http://tinyurl.com/2frj9u'); // returns http://www.forgedeuphoria.com/blog/ ?>
Download this code: revTinyURL-example.phps
Pretty neat, eh?
Questions, comments, something unclear? Leave a comment.




If you like TinyURL, you might also want to check out http://traceurl.com
TraceURL shortens any URL plus it tracks traffic to the page through the TraceURL shortened web site address, showing where the traffic originated. Count accesses and have the origin of the visitor displayed on a Google Map.