视频瑞士法郎的PHP正则表达式 [英] php regular expression for video swf

查看:210
本文介绍了视频瑞士法郎的PHP正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个对象/嵌入的HTML源获取视频网址。我阅读我可以使用正则表达式来得到它,但我和正则表达式是没有朋友的。

所以继承人我有什么:

 <?php 

函数src($ text){
$ text = str_replace('','',$ text );
$ text = str_replace('src =','',$ text);
$ temporary = explode('< embed',$ text);
$ temporary = $临时[1];
$ temporary = explode('',trim($ temporary));
return $ temporary [0];
}

$ html ='
< object width =180height =220>
< param name =movievalue =http://www.domain.com/video/video1。 swf>< / param>
< embed src =http://www.domain.com/video/video1.swftype =application / x-shockwave-flashwidth =180 HEIGHT = 220\" >< /嵌入>
将/对象>
;

回波SRC($ HTML);

这个工作正常,但在正则表达式中更好吗?



我正在使用灯


src 可能永远不在第一个属性中,所以这个将无法工作。



这是我推荐的:

$ p $函数src($ html) {
如果(的preg_match( '#<嵌入[^>]?* SRC = [\'](*)。?[\'](*)>?< /嵌入> #si',stripslashes($ html),$ src)){
return $ src [1];
}
return''; //或任何其他错误,如果你需要


echo src($ html);

会输出: http://www.domain.com/video/ video1.swf



[^>] 包含在括号内。 [^>]匹配>



[\' ] 匹配 src = src ='


(。)表示匹配任何字符。星号(*)表示零次或多次。而问号(?)意味着只要模式仍然匹配,就会贪婪地继续前进。把它放在一起,这意味着尝试和匹配任何字符,零次或多次,并获得尽可能多的,你可以

/ i <

http://en.wikipedia.org/wiki/Regular_expression



< a href =http://www.regular-expressions.info/reference.html =noreferrer> http://www.regular-expressions.info/reference.html


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

这篇关于视频瑞士法郎的PHP正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆