PHP preg_match regex (2)
Hi guys i'm having a problem, I have the following code:
<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="opaqu开发者_Python百科e" />
<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>
I need to get the value after screenfile=
for example this one: http://domain.com/file.jpg ,but I have no idea how can I do that, and I will also need to replace the width and height propertis.
This is a common question on SO and the answer is always the same: regular expressions are a poor choice for parsing or processing HTML or XML. There are many ways they can break down. PHP comes with at least three built-in HTML parsers that will be far more robust.
Take a look at Parse HTML With PHP And DOM and use something like:
$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;
}
Along the lines of:
$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];
}
Untested, but you get the idea. I would avoid using regex to parse HTML wherever possible, though in this case using regex alone could work (but since sample code and reality tend to diverge, I still recommend a parser based approach).
Use /screenfile=([^&]+)/
to find the value of screenfile. $1
will contain the desired value. Parsing html with regex is not a good idea though.
To change the width:
replace `/\bwidth="\d+"\b/` with width="423"
To change the height:
replace `/\bheight="\d+"\b/` with height="565"
精彩评论