Welcome Guest, Not a member yet? Register   Sign In
Regular expression with YouTube videos [solved]
#1

[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.
#2

[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.
#3

[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.




Theme © iAndrew 2016 - Forum software by © MyBB