屏幕使用preg_match刮取PHP

I am trying to create a php script that will retrieve the WOW factor numbers (right hand side) from this webpage http://forums.moneysavingexpert.com/forumdisplay.php?f=36 and store them in variables/array.

I have looked in the source code for the page and the values (ints) appear after this code "<div style="padding: 12px 0px 0px 0px;"><strong>"

I am trying to use preg_match currently just to retrieve 1 value (before I move onto retrieving multiple values), however I am having no luck. When I perform a var_dump there is nothing stored in my array. Also - I am not sure whether or not to escape the "s in the string above. If I do then var_dump prints out

array(0) { }

If I don't then var_dump prints out

NULL

The code I am using is below:

<html>
<head>
<title>
MSE Value Extractor
</title>
</head>
<body>
<?php

echo "Welcome to MSE deal finder!
";

$content = file_get_contents('http://forums.moneysavingexpert.com/forumdisplay.php?f=36');

preg_match('/<div style=\"padding: 12px 0px 0px 0px;\"><strong>(.)</', $content, $match);
var_dump($match);
$value = $match[1];

echo "Value obtained is $value 
";

?>

</body>
</html>

If anyone could comment on where I am going wrong, it would be hugely appreciated. I'm not that familiar with php.

Thanks in advance

I don't think using the style attribute is very semantic... here is a solution using DOMDocument and an xpath query :

<?php
$doc = new DOMDocument();
/* This page gives a loooot of warnings (probably because it's 
 * Money Saving Expert, not html expert)
 * Just ignore them with an @ 
 */
@$doc
  ->loadHTMLFile('http://forums.moneysavingexpert.com/forumdisplay.php?f=36');

$xpath = new DOMXPath($doc);
/* look for strong elements in td elements with a class attribute containing
 'popularity_threadbit_column' */
$list = $xpath
  ->evaluate("//td[contains(@class, 'popularity_threadbit_column')]//strong");
echo sprintf("found %d elements :" . PHP_EOL, $list->length);
foreach ($list as $element)
{
  echo $element->nodeValue . PHP_EOL;
}

Output:

$ php wow.php
found 27 elements :
5
0
0
0
0
1
0
922
112
0
290
661
390
18
2
51
0
31
163
163
46
33
103
50
90
0
109

Now you can try to write a regular expression that does the same, but I think it will be much uglier than the xpath expression we have here!

I'm not sure regex is the best way of doing this, although it could certainly fit the bill.

What about using a domparser, like http://simplehtmldom.sourceforge.net/, to traverse the HTML like you can in jQuery (if you are familiar with jQuery)?

it seems like you need an * after the (.) in the regex

you can test your regular expressions here: http://www.pagecolumn.com/tool/pregtest.htm

hope this helps.