tags:

views:

315

answers:

3

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="opaque" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="FlashVars" value="file=http://www.domain.com/file.flv&amp;screenfile=http://domain.com/file.jpg&amp;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&amp;screenfile=http://domain.com/file.jpg&amp;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.

Thanks, any help is more then welcome!

+1  A: 

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"
Amarghosh
The width and height regexp are not working or i'm doing something wrong?$patern_width = '/\bwidth="\d+"\b/';$replacement_width = '500';echo preg_replace($patern_width, $replacement_width, $content);
Uffo
The replacement string should be `width="500"`, not just `500`
Amarghosh
@Downvoters - I know it's not a good idea to use regex for parsing html - but I guess it's okay to use it in simple and straightforward situations like this one. Isn't it?
Amarghosh
+5  A: 

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;
}
cletus
+1 for the more thorough URL string handling. Nice.
Tomalak
Sure if he needed to parse html then regular expressions aren't the best tool, but all he needs to do is extract the value of screenfile. This is exactly what regular expressions were built for! The answer to every regexp question around here is "why don't you just run it through an html parser", but that's not always right.
Rob
+2  A: 

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

Tomalak