php regular expression for video swf

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

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

回答1:

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



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!