Search code examples
phppreg-matchfile-get-contents

PHP regular expressions


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);

Solution

  • 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