Kevin J
Kevin J

Reputation: 194

php preg_match expression to detect valid facebook post and video urls

How can I detect if a) the link is valid (structurally) and b) if it is a valid facebook photo or video link? Looking for the regular expressions for each case, not to determine if it's accessible or a valid destination.

Example photo link: https://www.facebook.com/949pediatricdentistry/photos/a.1438335623065047.1073741827.1438300469735229/1866032310295374/?type=3&theater

Example video link: https://www.facebook.com/chevrolet/videos/10153947517247296/

I've tried the following preg_match() statement which is close to detecting the different photo urls, but not fully passing the test:

preg_match('^(http(?:s?)?://www\.facebook\.com/(?:photo\.php\?fbid=\d+|([A-z0-9\.]+)\/photos(?:\/[0-9A-z].+)?\/(\d+)(?:.+)))?', 'https://www.facebook.com/photo.php?fbid=10201485039580806&set=a.2923144830611.2133032.1020548245&type=3&theater');

preg_match('^(http(?:s?)?://www\.facebook\.com/(?:photo\.php\?fbid=\d+|([A-z0-9\.]+)\/photos(?:\/[0-9A-z].+)?\/(\d+)(?:.+)))?', 'https://www.facebook.com/949pediatricdentistry/photos/a.1438335623065047.1073741827.1438300469735229/1866032310295374/?type=3&theater');

Upvotes: 0

Views: 384

Answers (1)

capcj
capcj

Reputation: 1535

You can try to check the host + facebook account + video/photo routine:

<?php
    $link = 'https://www.facebook.com/949pediatricdentistry/photos/a.1438335623065047.1073741827.1438300469735229/1866032310295374/?type=3&theater';
    $faceLink = explode('/', $link);
    if (($faceLink[2] == 'www.facebook.com' && $faceLink[3] == $username)) {
   //IS VALID
           if ($faceLink[4] == 'photos') {
             //photos routine
             } else if ($faceLink[4] == 'videos') {
               //videos routine
           }
    }
?>

Try by yourself before ask into Stack.

Upvotes: 0

Related Questions