-1

How to preg-match total_count from this code in php

{
   "data": [
      {
         "name": "Baptiste Chimay",
         "administrator": false,
         "id": "10153820811316460"
      },
   "summary": {
      "total_count": 4956
   }
}

i tried but it didnt get any data

$url = ('the url i want');
                $result = json_decode($url);
    $likes = preg_match("'total_count (.*?),'si", file_get_contents($url), $matches);
print_r($url);
Dr.Mezo
  • 807
  • 3
  • 10
  • 25

2 Answers2

2

Thats json data, you can use json_decode to create a php array:

$data = json_decode(file_get_contents($myurl), true);

echo $data['summary']['total_count']; //outputs 4956
Steve
  • 20,703
  • 5
  • 41
  • 67
0

Just escape the quotes:

preg_match("/total_count\"\:(.*?)/i", file_get_contents($myurl), $matches);
//               here __^

But, you don't need to escape the selmicolon

preg_match("/total_count\":(.*?)/i", file_get_contents($myurl), $matches);

you could also replace the double quotes by single quotes:

preg_match('/total_count":(.*?)/i', file_get_contents($myurl), $matches);

and change the .*? to [^"]+

preg_match("/total_count\":([^"]+)/i", file_get_contents($myurl), $matches);

you could also replace the double quotes by single quotes:

preg_match('/total_count":(\d+)/i', file_get_contents($myurl), $matches);
Toto
  • 89,455
  • 62
  • 89
  • 125