Is there any PHP function that defines whether a given link is a video, picture or neither.
For example:
$link = www.example.com/342kddd23
$a = phpfunction($link);
If $link
is a video link; $a = 1,
if $link
is a picture link $a = 2
, if $link
is not defined (it could be a normal link) $a = 0
.
Is that possible? If so, are there any existing functions like that?
I don't believe such a library exists (there's no built-in function I'm aware of).
You could of course attempt to develop your own, but you'll endlessly be chasing your tail if you plan to go beyond simply checking to see if there's a known file extension such as ".mp4", ".png", etc. due to the fact that you'll need to construct an increasingly large list of matching URL schemas. (This would also imply that a given link may only return a single type of content, which is often not the case.)
Assuming that you're looking for video and image files themselves (and not simply URLs that contain a video, etc.) an alternative (more accurate) approach would be request each link (perhaps using cURL with CURLOPT_HEADER enabled), see what content headers are returned (specifically "Content-Type
") and take things from there.
Update
As a follow-up based on @smassey's excellent comment, setting the CURLOPT_NOBODY
option (once again via curl_setopt) will send a HEAD
request, hence ensuring you don't actually download any of the content beyond the response headers themselves.
It doesn't exist but you can make your own function. It can be based on domain or extension or both...
For example you can list most common video and pics hosting and check if domain correspond.
Very basic:
$videos_services = array('www.youtube.com', 'www.vimeo.com');
$pics_services = array('www.flickr.com', 'www.imageshack.us');
function urlType($myUrl)
{
$url = parse_url($myUrl);
$host = $url['host'];
if(in_array($host, $videos_services))
{
return 1;
}
elseif(in_array($host, $pics_services))
{
return 2;
}
else
{
return 0;
}
}
becareful, it's a very simple example but you have to deal with url like http://youtube.com or http://www.youtu.be etc.