我想从对象/嵌入 html 源获取视频 url。我读到我可以使用正则表达式来获取它,但我和正则表达式不是朋友
这就是我所拥有的:
<?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);
这可行,但是在正则表达式中更好吗?
我正在使用灯
正则表达式更适合这种情况,因为src
可能永远不会位于第一个属性,因此这不起作用。
这是我的建议:
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);
将输出:http://www.domain.com/video/video1.swf
[^>]
匹配未包含在方括号内的单个字符。 [^>] 匹配除>
["\']
火柴src="
or src='
(.*?)
点(.)表示匹配任意字符。星号 (*) 表示零次或多次。问号(?)意味着贪婪,只要模式仍然匹配就继续下去。把它们放在一起,这意味着尝试匹配任何字符,零次或多次,并获得尽可能多的结果
/i
不区分大小写
以下是更多信息:
http://en.wikipedia.org/wiki/Regular_expression http://en.wikipedia.org/wiki/Regular_expression
http://www.regular-expressions.info/reference.html http://www.regular-expressions.info/reference.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)