2

I have a string that looks like 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 only need the value for the country_name from that string.

so I 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 is nothing being echoed in the $result

Could someone please advise on this issue?

1
  • 1
    this looks like a json string. you should find a library that can convert it to an object and extract the value from that object. This is way easier than string parsing Commented Oct 27, 2015 at 15:23

4 Answers 4

5
$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 you have there is a JSON string.

JSON stands for JavaScript Object Notation. PHP can decode it into an Array or Object via json_decode($string, FALSE);

The 2nd parameter by default is FALSE, which means it will convert the string into an object, which you can then access as I showed you above.

Sign up to request clarification or add additional context in comments.

Comments

2

If for some reason you don't want to use JSON you can give the following a try. Note that using JSON is the recommended way doing this task.

$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}';

$temp = explode('"country_name":', $country); //Explode initial string
$temp_country = explode(',', $temp[1]); //Get only country name
$country_name = str_replace('"', ' ', $temp_country[0]); //Remove double quotes
echo $country_name;

Result:

Ireland

5 Comments

In my honest opinion, when you have a JSON string, you shouldn't even think about treating it as a regular string and use explodes and such. Hence the downvote.
This is why i didn't just write down the code but i first had the phrase: " If for some reason you don't want to use JSON " !
I also upvoted the accepted answer because it was a good answer but it's nice to provide other options too (just in case).
Alright. Edit the answer so I can remove my downvote.
I think it looks better now giving that note.
0

Try this:

<?php
$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}')
$result=$country->country_name;
echo $result;
?>

4 Comments

Works, but why the {}? Not needed here.
@AbraCadaver I tried to implement as demonstrated in the Manual. Check it out: php.net/manual/en/function.json-decode.php
That's for invalid property names, country_name is perfectly valid.
Uh... You don't want the quotes either :-(
0

This looks like a json string. Read more about JSON.

Use it like this:

$foo = '{"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}';
$bar = json_decode($foo, true);
echo $bar['country_name'];

This can be used with any key from that string (eg ip, city)

More about json_decode.

1 Comment

Notice: Undefined variable: a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.