iwant to get the video url from a object/embed html source. i read i can use regular expression to get it but me and regular expression are no friends
so heres what i have:
<?php
function src($text) {
$text = str_replace('"', '', $text);
$text = str_replace('src=', '', $text);
$temporary = explode('<embed', $text);
$temporary = $temporary[1];
$temporary = explode(' ', trim($temporary));
return $temporary[0];
}
$html = '
<object width="180" height="220">
<param name="movie" value="http://www.domain.com/video/video1.swf"></param>
<embed src="http://www.domain.com/video/video1.swf" type="application/x-shockwave-flash" width="180" height="220"></embed>
</object>
';
echo src($html);
this works but is it better in regular expression?
i am using lamp
A regular expression is better for this case because the src
might never be at the first attribute, therefore this won't work.
Here's what I recommend:
function src($html) {
if(preg_match('#<embed[^>]*?src=["\'](.*?)["\'](.*?)></embed>#si', stripslashes($html), $src)) {
return $src[1];
}
return ''; // or any other error if you need
}
echo src($html);
will output: http://www.domain.com/video/video1.swf
[^>]
matches a single character that is not contained within the brackets. [^>] matches any character other than >
["\']
matches src="
or src='
(.*?)
Dot (.) means match any character. Star (*) means zero or more times. And question mark (?) means be greedy and keep going as long as the pattern still matches. Put it all together, it means try and match any character, zero or more times, and get as many as you can
/i
is case insensitive
Here's more info:
http://en.wikipedia.org/wiki/Regular_expression
http://www.regular-expressions.info/reference.html
Why don't you use a DOM parser; it's designed to do this kind of work.
$dom = new DOMDocument;
$dom->loadHTML($html);
$embed = $dom->getElementsByTagName('embed');
if ($embed->length) {
$embed = $embed->item(0);
if ($embed->hasAttribute('src')) {
$src = $embed->getAttribute('src');
// `$src` holds the `src` attribute of the `embed` element.
}
}
CodePad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With