H.HISTORY
H.HISTORY

Reputation: 518

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

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?

Upvotes: 2

Views: 218

Answers (4)

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

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

Upvotes: 2

FirstOne
FirstOne

Reputation: 6217

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.

Upvotes: 0

Manikiran
Manikiran

Reputation: 1

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

Upvotes: 0

Andr&#233; Catita
Andr&#233; Catita

Reputation: 1323

$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.

Upvotes: 5

Related Questions