Reversing TinyURL

This is a neat little way to "reverse" TinyURL's code. It dosn't really reverse the url but seeks the URL inside the page.

Here's our whole code:

CODE
<?php
function tinyurl_reverse($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];
}
?>



First the function splits the url out starting with .com, next it gets the "html" or " source" of the page with the link.


CODE
    $url = explode('.com/', $url);
    $url = 'http://preview.tinyurl.com/'.$url[1];



Next finds the redirection code (redirecturl) inside the page.


CODE
    $preview = file_get_contents($url);
    preg_match('/redirecturl" href="(.*)">/', $preview, $matches);



And last but not least it gets the redirecturl URL and displays it.


CODE
    return $matches[1];



Pretty neat eh.

I would have to give credit to logankoester at dzone for this snippet.

Thursday August 16, 2007 - 2994 reads