Why is my preg_match result doubled?

Slowly but surely, I am building a site for personal use, where I am looking for the name of the film, and it returns me a metacritical rating. Here is 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 

If he should return only 83, not 8383.

I pointed the way to where the metacritical rating is located, because I need to capture this information. I'm not sure if this is correct? Maybe very rough work, I do not understand preg_match very well, the documentation on the Internet did not help.

Can anyone help me out?

+4
source share
3 answers

You only need the $score[1] echo, because it is your first captured subpattern in brackets that will be included.

Read the documentation for preg_match

+10
source

FYI:

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

The manual reads:

If matches are indicated, they are populated with search results. $ matches [0] will contain text that matches the full pattern, $ matches [1] will have text that matches the first captured subpattern in brackets, etc.

+5
source

use print_r for matches, but I think it prints backlink value in parentheses like second

+1
source

Source: https://habr.com/ru/post/1384807/


All Articles