CodeIgniter Forums
Regular expression with YouTube videos [solved] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Regular expression with YouTube videos [solved] (/showthread.php?tid=4835)



Regular expression with YouTube videos [solved] - El Forum - 12-18-2007

[eluser]codelearn[/eluser]
I wanted to throw out a question to you guys because I'm not much of a regular expression wizard.

I have a YouTube video embed code which I need to validate before placing on my site:

Code:
&lt;object width="425" height="355"&gt;<param name="movie" value="http://www.youtube.com/v/5PsnxDQvQpw&rel=1"></param><param name="wmode" value="transparent"></param>&lt;embed src="http://www.youtube.com/v/5PsnxDQvQpw&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;

How do I use regular expression to just pull out the link (http://www.youtube.com/v/5PsnxDQvQpw&rel=1) from that code? Is this easily done?

Thanks for any help. =)

EDIT: A tiny bit of fiddling with the regular expression below did the trick. Thanks.


Regular expression with YouTube videos [solved] - El Forum - 12-18-2007

[eluser]Nick Husher[/eluser]
Something to the effect of /\w+src="(.+)"/ should do it. You have to figure out how to make that a nongreedy match, though, and I've forgotten how to do that.


Regular expression with YouTube videos [solved] - El Forum - 12-18-2007

[eluser]ejangi[/eluser]
I just very quickly threw this together and it seems to grab just the URL:
Code:
$string = '&lt;object width="425" height="355"&gt;<param name="movie" value="http://www.youtube.com/v/5PsnxDQvQpw&rel=1"></param><param name="wmode" value="transparent"></param>&lt;embed src="http://www.youtube.com/v/5PsnxDQvQpw&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;';
preg_match('/http:\/\/www\.youtube[^"]+/', $string, $matches);
$url = $matches[0];

You may want to check it on a few different movies though just to be sure.