Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my preg_match result getting doubled?

Tags:

php

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?

like image 569
Sara Avatar asked Dec 05 '11 23:12

Sara


1 Answers

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

Read documentation for preg_match

like image 85
Cyclonecode Avatar answered Sep 22 '22 11:09

Cyclonecode