PHP: Extract specific word after specific word in a string? -


i have string looks this:

{"ip":"xx.xx.xx","country_code":"ie","country_name":"ireland","region_code":"l","region_name":"leinster","city":"dublin","zip_code":"","time_zone":"europe/dublin","latitude":53.333,"longitude":-6.249,"metro_code":0} 

i need value country_name string.

so tried this:

$country = '{"ip":"xx.xx.xx","country_code":"ie","country_name":"ireland","region_code":"l","region_name":"leinster","city":"dublin","zip_code":"","time_zone":"europe/dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';  if (preg_match('#^country_name: ([^\s]+)#m', $country, $match)) {     $result = $match[1]; }  echo $result; 

but there nothing being echoed in $result

could please advise on issue?

$country = json_decode('{"ip":"xx.xx.xx","country_code":"ie","country_name":"ireland","region_code":"l","region_name":"leinster","city":"dublin","zip_code":"","time_zone":"europe/dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}');  echo $country->country_name; 

what have there json string.

json stands javascript object notation. php can decode array or object via json_decode($string, false);

the 2nd parameter default false, means convert string object, can access showed above.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -