2

I am making a video playback website and I need to use a html5 video player to play local videos and youtube api and vimeo api to play vimeo videos. THE problem is I want to filter the link entered by the user for all 3 conditions. I tried some regex as I never used it before but was not successful. Can anyone help me giving a example showing how to separate those links for making 3 conditions

  1. taking out the word "youtube" out of a youtube link so that i could make a condition statement

  2. same way taking out .mp4 and .ogg extension from the local video so that it can be used to be played in a html5 video player

  3. same way extract the word "vimeo" from a vimeo link so that I can use a vimeo api to play vimeo videos.

Can anyone help?

For example we input a youtube link

http://www.youtube.com/watch?v=example

Then how to get the word "youtube" out of this link?

And I uploaded a local video with file name

example.mp4

Then how can I get the extensoon mp4 from that?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Sakshi Sharma
  • 1,414
  • 4
  • 20
  • 39

1 Answers1

2

This is adapated from a php class I wrote some times ago :

<?php

function identifyService ( $url )
{
    $url = preg_replace('#\#.*$#', '', trim($url));

    $services_regexp = array(
        "#^\w+\.(?P<format>[a-zA-Z0-9]{2,5})#"                     => 'local',
        '#vimeo\.com\/(?P<id>[0-9]*)[\/\?]?#i'                     => 'vimeo',
        '#youtube\.[a-z]{0,5}/.*[\?&]?v(?:\/|=)?(?P<id>[^&]*)#i'   => 'youtube'
    );

    foreach ( $services_regexp as $pattern => $service ) {
        if ( preg_match ( $pattern, $url, $matches ) ) {
            return ( $service === 'local' ) ? $matches['format']  : $service;
        }
    }

    return false;

}


$url = "foo.ogg";
// $url = "bar.mp4";
// $url = "http://www.youtube.com/watch?v=_rqR8_mOp_A";
// $url = "http://vimeo.com/39044814";
$service  = identifyService( $url );
var_dump( $service );


?>

Hope it helps.

Denis
  • 1,093
  • 5
  • 15
  • If you want to retrieve other information, such as the video files, author, title, and so on, it could work with this class but with some adaptations, as the services API may have changed. – Denis May 26 '12 at 19:38
  • thanks a lot for hepling me with youtube and vimeo yes it cleared my 70 % of problem but what to do with the local videos . is there anyway to get a extention out of a link such as example.mp4 and get """mp4 outta that – Sakshi Sharma May 26 '12 at 19:39
  • I edited my answer to address more specifically your question. If it is a local video (say, "foo.mp4" or "bar.ogg"), it will return the file extension, otherwise it will return you the name of the service. – Denis May 26 '12 at 20:03
  • omg @Denis U saved me from one more sleepless night . thanks a lot for the exact help i required . really useful. hey can u link me with the rest of the regex code of other websites u gave earlier , it might be usefull in future projects . thanks a lot – Sakshi Sharma May 26 '12 at 20:05
  • 1
    Check the first link of my answer. Also, if you’re interested in learning regex, this question might be useful : http://stackoverflow.com/questions/4736/learning-regular-expressions – Denis May 26 '12 at 20:08
  • this is just so nice of you , u liked me for learning regex a bit , thanks a lot friend – Sakshi Sharma May 26 '12 at 20:10
  • Well, I'm glad it helped but you should really try to learn regular expressions. I gave you a solution straight, but actually learning how to use RegExp is a must for a developer. – Denis May 26 '12 at 20:17
  • hey denis the code above for local is working for all extensions such as mp4,flv,oog,wmv but it does not work for .m4v ny solution on that? and it also dosen't count up spaces for eg 31919409066TiK ToK.mp4 it returns false instead of local – Sakshi Sharma May 27 '12 at 08:32
  • There’s no reason it won’t match .m4v. To match spaces as well, try using `"#^[\w\s]+\.(?P[a-zA-Z0-9]{2,5})#"` – Denis May 27 '12 at 08:53
  • thanks for the reply friend but the file name 31919409066TiK ToK.m4v is not detected as local its returning false . m4v is not detected dnt knw why . hey because of u i tried some regex and was successfull is modifying the youtube for the new yotu.be url thanks , but m stuck at my porject in this rpoblem tht it is not detecting m4v – Sakshi Sharma May 27 '12 at 08:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11794/discussion-between-denis-hovart-and-sakshi-sharma) – Denis May 27 '12 at 09:01
  • also the code for the space is not working i am trying to use it with a file name 31261335046Agent Vinod.mp4 and it didnt worked – Sakshi Sharma May 27 '12 at 09:01
  • hey denis soory for asking u one more query of mine the code gave me in this i modified it by if and else statements that if ( preg_match ( $pattern, $link ) ) { if($service=='youtube'||$service=='local'){}else {do this} else {return this} but its returning three times ?why so – Sakshi Sharma May 27 '12 at 09:51
  • it here nay way to add a regex for any other video expect then other, youtube,vimeo? may it be any comes in others category? – Sakshi Sharma May 27 '12 at 10:35
  • Please read this "What have you tried? " link posted by Cole Johnson. I already helped you and gave you the answer you wanted - I won't do anything more now. Have you at least checked the links I gave you ? – Denis May 27 '12 at 10:49
  • yes i have friend but its not possible for me to learn all regex in one day , its a urgent project m working on its ok u helped a lot thought that link i was able to make a regext query for yout.be link also but i am not able to separate a else for others video such that it does not have youtube, vimeo,local m trying from 5 hours and its returning 3 times thats why i asked you again , but its okh – Sakshi Sharma May 27 '12 at 10:57