PHP: how to check if Youtube or vimeo's URL is

How can I write a function to check if the provided youtube or vimeo urls are?

For example, I have two URLs that I store in the database as strings,

http://vimeo.com/24456787 http://www.youtube.com/watch?v=rj18UQjPpGA&feature=player_embedded 

If the youtube url, I rewrote the url,

 http://www.youtube.com/embed/rj18UQjPpGA?rel=0&wmode=transparent 

If the vimeo url, I would rewrite this url,

 http://vimeo.com/moogaloop.swf?clip_id=24456787 

Thanks.

+6
source share
8 answers

Regular expressions work well for this type of thing, but often strpos or substr work faster. Check out the PHP documentation for preg_match() . Below examples there is a note for this thing.

Here is the prototype code:

 function videoType($url) { if (strpos($url, 'youtube') > 0) { return 'youtube'; } elseif (strpos($url, 'vimeo') > 0) { return 'vimeo'; } else { return 'unknown'; } } 

Obviously returning a string is not a good idea, but you get the point. Substitute your own business logic.


As others noted in the comments, this is a quick and dirty solution that does a great job with edge cases. If the url contains "youtube" (example.com/youtube), it will return a false result. The parse_url() solution mentioned below is a much more robust solution.

+10
source

Use the parse_url function to split the URL up, and then just do the usual checks.

 $url = 'http://www.youtube.com/watch?v=rj18UQjPpGA&feature=player_embedded'; $parsed = parse_url($url); 

Gives you this array

 array 'scheme' => string 'http' (length=4) 'host' => string 'www.youtube.com' (length=15) 'path' => string '/watch' (length=6) 'query' => string 'v=rj18UQjPpGA&feature=player_embedded' (length=37) 
+24
source

Surprisingly, no one mentioned that youtube URLs can also contain the "youtu.be" domain, in which case all of the above solutions must be adapted to satisfy this possibility.

+11
source

You can use preg_match() :

 $u1="http://vimeo.com/24456787"; $u2="http://www.youtube.com/watch?v=rj18UQjPpGA&feature=player_embedded"; if(preg_match('/http:\/\/(www\.)*vimeo\.com\/.*/',$u1)){ // do vimeo stuff echo "Vimeo URL found!\n"; } if(preg_match('/http:\/\/(www\.)*youtube\.com\/.*/',$u2)){ // do youtube stuff echo "YouTube URL found!\n"; } 
+3
source

Since all you want to do is check for the presence of a string, use stripos . If it doesn't have youtube.com or vimeo.com, the url is incorrect, right? stripos is also not case sensitive.

 if(stripos($url,'youtu')===false){ //must be vimeo } else { //is youtube } 
+2
source

You can try your solution:

 function checkServer( $domains=array(), $url ) { foreach ( $domains as $domain ) { if ( strpos($url, $domain ) > 0) { return true; } else { return false; } } } 

Using:

 if( checkServer(array("youtube.com","youtu.be"), $url ) ) { //is Youtube url } elseif( checkServer(array("vimeo.com"), $url ) ) { //is Vimeo } elseif ( checkServer(array("any domain"), $url ) ) { //is Any Domain }else { //unknow domain } 
+1
source

I recently wrote this function to do just that, hope this is useful to someone:

  /** * [determineVideoUrlType used to determine what kind of url is being submitted here] * @param string $url either a YouTube or Vimeo URL string * @return array will return either "youtube","vimeo" or "none" and also the video id from the url */ public function determineVideoUrlType($url) { $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/'; $has_match_youtube = preg_match($yt_rx, $url, $yt_matches); $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([az]*\/)*([‌​0-9]{6,11})[?]?.*/'; $has_match_vimeo = preg_match($vm_rx, $url, $vm_matches); //Then we want the video id which is: if($has_match_youtube) { $video_id = $yt_matches[5]; $type = 'youtube'; } elseif($has_match_vimeo) { $video_id = $vm_matches[5]; $type = 'vimeo'; } else { $video_id = 0; $type = 'none'; } $data['video_id'] = $video_id; $data['video_type'] = $type; return $data; } 
+1
source

Use regular expressions . What language do you use?

Edit: noticed that your tag was php. It will be something like this:

 <?php // get host name from URL preg_match('@^(?:http://)?([^/]+)@i', "http://www.youtube.com/index.html", $matches); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); echo {$matches[0]}\n"; //youtube.com ?> 
0
source

Source: https://habr.com/ru/post/892284/


All Articles