PHP preg_match正则表达式(2)

Uff*_*ffo 0 php regex

嗨,大家好我有问题,我有以下代码:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0" width="320" height="240">
<param name="movie" value="http://www.domain.com" />
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="FlashVars" value="file=http://www.domain.com/file.flv&screenfile=http://domain.com/file.jpg&dom=domain.com" />
<embed src="http://www.domain.com" width="320" height="240" bgcolor="#000000" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowfullscreen="true" flashvars="file=http://domain.com/file.flv&screenfile=http://domain.com/file.jpg&dom=domain.com" />
</object>
Run Code Online (Sandbox Code Playgroud)

我需要获得以下值后的值screenfile=:http://domain.com/file.jpg,但我不知道我该怎么做,我还需要更换宽度和高度属性.

cle*_*tus 5

这是关于SO的常见问题,答案总是相同的:正则表达式是解析或处理HTML或XML的不良选择.他们有很多方法可以打破.PHP附带至少三个内置的HTML解析器,它们将更加强大.

看看使用PHP和DOM解析HTML并使用类似的东西:

$html = new DomDocument;
$html->loadHTML($source); 
$html->preserveWhiteSpace = false; 
$params = $html->getElementsByTagName('param');
foreach ($params as $param) {
  if ($param->getAttribute('name') == 'FlashVars') {
    $params = decode_query_string($param->getAttribute('value'));
    $screen_file = $params['screenfile'];
  }
}
$embeds = $html->getElementsByTagName('embed');
$embed = $embed[0];
$embed->setAttribute('height', 300);
$embed->setAttribute('width', 400);
$raw_html = $html->saveHTML();

function decode_query_string($url) {
  $parts = parse_url($url);
  $query_string = $parts['query'];
  $vars = explode('&', $query_string);
  $ret = array();
  foreach ($vars as $var) {
    list($key, $value) = explode('=', $var, 2);
    $ret[urldecode($key)][] = urldecode($value);
  }
  return $ret;
}
Run Code Online (Sandbox Code Playgroud)