PHP预匹配正则表达式(2)

PHP预匹配正则表达式(2),php,regex,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.d

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

<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>

我需要在
screenfile=
之后获取值,例如这个:,但我不知道如何才能做到这一点,我还需要替换宽度和高度属性。

使用
/screenfile=([^&]+)/
来查找screenfile的值<代码>$1将包含所需的值。不过,用正则表达式解析html不是一个好主意

要更改宽度,请执行以下操作:

  replace `/\bwidth="\d+"\b/` with width="423"
要更改高度,请执行以下操作:

  replace `/\bheight="\d+"\b/` with height="565"

这是一个常见的问题,答案总是一样的:正则表达式在解析或处理HTML或XML时是一个糟糕的选择。有很多方法可以让他们崩溃。PHP附带了至少三个内置的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;
}
大致如下:

$html = '<your HTML here>';

$dom = new DOMDocument;
$dom->loadHTML($html);

$xpath  = new DOMXPath($dom);
$result = $xpath->query('//object/param[@name = "FlashVars"][1]/@value');

foreach ($result as $node) {  // there should only be one
  preg_match(/screnfile=([^&]+)/, $node->nodeValue, $matches);
  print $matches[1];
}
$html='';
$dom=新的DOMDocument;
$dom->loadHTML($html);
$xpath=newdomxpath($dom);
$result=$xpath->query('//object/param[@name=“FlashVars”][1]/@value');
foreach($result as$node){//应该只有一个
preg_match(/screnfile=([^&]+)/,$node->nodeValue,$matches);
打印$matches[1];
}

未经测试,但你明白了。我会尽可能避免使用正则表达式解析HTML,尽管在这种情况下单独使用正则表达式可能会起作用(但由于示例代码和实际情况往往会出现分歧,我仍然建议使用基于解析器的方法)。

正则表达式的宽度和高度不起作用,或者我做错了什么$patern_width='/\bwidth=“\d+”\b/”$替换_宽度='500';echo preg\u replace($paten\u width,$replacement\u width,$content);替换字符串应该是
width=“500”
,而不仅仅是
500
@downvorters-我知道使用正则表达式解析html不是一个好主意-但我想在像这样简单明了的情况下使用它是可以的。不是吗?当然,如果他需要解析html,那么正则表达式不是最好的工具,但他所需要做的就是提取screenfile的值。这正是正则表达式构建的目的!这里每个regexp问题的答案都是“为什么不通过html解析器运行它”,但这并不总是正确的。