0

How can I extract youtube embed code from a paragraph of text ?

I tried preg_match_all() function, but its not returning the complete emebed code, any suggestions ?

I am trying to get the embed code completly like

 <iframe width="560" height="315" src="//www.youtube.com/embed/o66nNnGZ5Rg" frameborder="0"  allowfullscreen></iframe>
Joyal
  • 2,587
  • 3
  • 30
  • 45

1 Answers1

1

Does this suit you? It will fully match any iframe tag with a src of anything containing youtube.com.

preg_match_all('#<iframe[^>]+src="[^"]+youtube\.com[^"]+"[^>]*>[\s]*</iframe>#', $text, $matches);
Niall
  • 457
  • 3
  • 8