I want to get Video ID for YouTube or Vimeo via its Embed code or from URL, Any solution to do this with PHP ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use preg_match to get the IDs. I will cover the expressions themselves later in this answer, but here is the basic idea of how to use preg_match:
Here is a breakdown of the expressions for each type of possible input you asked about. I included a link for each showing some test cases and the results.
For YouTube URLs such as
http://www.youtube.com/watch?v=89OpN_277yY, you could use this expression:YouTube embed codes can either look like this (some extraneous stuff clipped):
Or like this:
So an expression to get the ID from either style would be this:
Vimeo URLs look like
http://vimeo.com/<integer>, as far as I can tell. The lowest I found was simplyhttp://vimeo.com/2, and I don’t know if there’s an upper limit, but I’ll assume for now that it’s limited to 10 digits. Hopefully someone can correct me if they are aware of the details. This expression could be used:Vimeo embed code takes this form:
So you could use this expression:
Alternately, if the length of the numbers may eventually exceed 10, you could use:
Bear in mind that the
"will need to be escaped with a\if you are enclosing the expression in double quotes.In summary, I’m not sure how you wanted to implement this, but you could either combine all expressions with
|, or you could match each one separately. Add a comment to this answer if you want me to provide further details on how to combine the expressions.