1

Possible Duplicate:
php regex - find all youtube video ids in string

How can I get the youtube Id from the embed using Regex, even it is in old format or ifarme

example

<iframe width="560" height="315" src="http://www.youtube.com/embed/ghc8cYOA1Vo" frameborder="0" allowfullscreen></iframe>

or

<object width="560" height="315"><param name="movie" value="http://www.youtube.com/v/ghc8cYOA1Vo?hl=en_US&amp;version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ghc8cYOA1Vo?hl=en_US&amp;version=3" type="application/x-shockwave-flash" width="560" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>

please advice,

Community
  • 1
  • 1
Yasser-Farag
  • 592
  • 4
  • 9
  • 28

3 Answers3

4
youtube.com/((v|embed)/)?[a-zA-Z0-9]+

Youtube has recently gone from 10-character IDs to 11 characters, and it's possible that they may eventually increase that number.

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
1

Using the regexp youtube[.]com/(v|embed)/([^"?]+), the YouTube ID will be captured in the second group.

DEMO

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 1
    Yes. Because it clearly states there that you must escape forward slashes! But that's particular to Rubular. The regular expression provided definitely works. – steinar Feb 04 '13 at 14:29
1

I tested this regex ((v|embed))\/?[a-zA-Z0-9_-]+ and it works fine

Yasser-Farag
  • 592
  • 4
  • 9
  • 28
  • **1°)** How are you getting the YouTube ID with this regexp? **2°)** This is really a bad regexp, I'm not sure you know the common special chars... **3°)** `helloworlv-`, `va` and `v-_-` will match. I really recommand you to use mine. – sp00m Feb 04 '13 at 15:01