phpparse-url

Parse Adf.ly URL using PHP


I have a link like that: http://adf.ly/2323070/http://www.fileflyer.com/view/O33TOYUAFMIFF

And I want to get only this part of the URL: http://www.fileflyer.com/view/O33TOYUAFMIFF

Note The URL number can be change, so the syntax of the URL is: http://adf.ly/{NUMBERS}/{URL}

How can I parse this URL?

Thanks! and sorry for my English


Solution

  • A simple regex of #https?://(www\.)?adf\.ly/\d+/(.*)#i should do the trick. This also supports https://adf.ly` (in case it exists) and http://www.adf.ly.

    <?php
        $url = "http://‌‌‌‌‌‌"."adf.ly/2323070/http://www.fileflyer.com/view/O33TOYUAFMIFF";
        preg_match("#https?://(www\.)?adf\.ly/\d+/(.*)#i", $url, $matches);
        var_dump($matches[2]);
    ?>
    

    outputs

    string(43) "http://www.fileflyer.com/view/O33TOYUAFMIFF"