tags:

views:

31

answers:

2

My users can send links from popular file hosts like Rapidshare, Megaupload, Hotfile and FileFactory. I need to somehow find out what filehost they sent the link from and use the correct class for it appropriately.

For example, if I sent a Rapidshare link in a form on my web page, I need to somehow cycle through each file host that I allow until I find the text rapidshare.com, then I know the user has posted a Rapidshare link.

Perhaps a PHP example:

switch($_POST['link'])
{
    case strstr($_POST['link'], 'rapidshare.com'):
        // the link is a Rapidshare one
        break;

    case strstr($_POST['link'], 'megaupload.com'):
        // the link is a Megaupload one
        break;

    case strstr($_POST['link'], 'hotfile.com'):
        // the link is a Hotfile one
        break;

    case strstr($_POST['link'], 'filefactory.com'):
        // the link is a Filefactory one
        break;
}

However, I know for a fact this isn't correct and I'd rather not use a huge IF statement if I can help it.

Does anyone have any solution to this problem?

If you need me to explain more I can try, English isn't my native language so it's kinda hard.

Thanks all.

+1  A: 

Nevermind guys, I used this:

$sentLink = trim($_POST['link']);

$host = parse_url($sentLink, PHP_URL_HOST);

switch($host)
{
    case 'rapidshare.com':
        echo "RS";
        break;
    case 'megaupload.com':
        echo "MU";
        break;
    case 'hotfile.com':
        echo "HF";
        break;
    case 'filefactory.com':
        echo "FF";
        break;
    default:
        echo "WTF! D:";
}

First time I've heard of parse_url :)

blerh
See my post about making sure to parse out the www. that can be returned from parse_url.
Ballsacian1
+1  A: 

According to http://stackoverflow.com/questions/1813599/php-regex-hostname-extraction you want to make sure you check for the www. portion of th eurl as parse_url can sometimes return that as well.

$sentLink = trim($_POST['link']);

$host = array_shift( explode( '.', str_replace('www.', '', parse_url( $sentLink , PHP_URL_HOST )) ) );

switch($host)
{
    case 'rapidshare.com':
        echo "RS";
        break;
    case 'megaupload.com':
        echo "MU";
        break;
    case 'hotfile.com':
        echo "HF";
        break;
    case 'filefactory.com':
        echo "FF";
        break;
    default:
        echo "WTF! D:";
}
Ballsacian1
Thanks, didn't think about the www part :)
blerh