5

Slowly, but surely, I'm assembling a website for personal usage where I search for the name of a movie and it returns me the metacritic rating of it. Here's what I got so far:

$web = file_get_contents("http://www.metacritic.com/movie/the-lion-king"); 

preg_match(
    '/<span class="score_value" property="v:average">(.*?)<\/span>/s',
    $web,
    $match
 );

foreach ($match as $score) {
    $sc = $score;
    echo $sc;
}

Result:

8383

Where it should return only 83 and not 8383.

I pointed the way to where the metacritic rating is located because I need to grab that information. I'm not sure whether or not it's correct? May be some very crude work, I don't understand preg_match very well, the documentation online did not help a bit.

Can someone please help me?

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Sara
  • 83
  • 1
  • 1
  • 7
  • http://stackoverflow.com/a/1732454/576139 – Chris Eberle Dec 05 '11 at 23:34
  • Gosh, so regex can't parse html? Thanks Chris! – Sara Dec 05 '11 at 23:39
  • 1
    Since you're new to SO I'm going to give you a friendly suggestion. You should choose an answer below to accept. I'm going to recommend Krister's since that answer was posted before mine and is just as valid. – Tim G Dec 05 '11 at 23:55
  • 1
    Thanks for the suggestion Tim, I did accept his answer. I couldn't do it before because there was a timer, silly timer. – Sara Dec 06 '11 at 00:06

3 Answers3

11

You should only echo out $score[1] because that is where your first captured parenthesized subpattern will go.

Read documentation for preg_match

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • 1
    So that's where I was wrong, I was doing a loop unnecessarily wasn't I? I'm ashamed. Thank you Krister!! – Sara Dec 05 '11 at 23:40
5

FYI:

array(2) {
  [0]=>
  string(56) "<span class="score_value" property="v:average">83</span>"
  [1]=>
  string(2) "83"
}

Manual reads:

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

Tim G
  • 1,812
  • 12
  • 25
  • Ohhhhhhhhhhhh, so that's why my loop was doubling it. I don't understand how it showed only 83 for [0], shouldn't it show the whole `"83" ` ? – Sara Dec 05 '11 at 23:41
  • it did. but the span is invisible in output. :) It shows up if you do a view source. – Tim G Dec 05 '11 at 23:42
1

use print_r on matches to be sure but I think it is printing the back-referenced value in parentheses as the second one

hackartist
  • 5,172
  • 4
  • 33
  • 48