Removing information from a string

I'm using an API which uses the users IP address to find out the users Country Location and City, however I have never used regex before and I have no Idea how to extract the other information I want.

    $location=file_get_contents('http://api.hostip.info/get_html.php?ip='.$ip);

This line returns 'Country: UNITED KINGDOM (GB) City: Edinburgh IP: 80.192.82.75' I have managed to extract the IP address using regex, but have no idea how to remove the Country and City into separate variables ($country = , $city =). Here is the code below I have so far.

 $ip=$_SERVER['REMOTE_ADDR'];
 $location=file_get_contents('http://api.hostip.info/get_html.php?ip='.$ip);
 preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $location, $matches); 
 $ip = $matches[0]; 

Answers


Taking the suggestion from millimoose it would look like this:

$jstring = file_get_contents('http://api.hostip.info/get_json.php?ip='.$ip);
$ipinfo = json_decode($jstring);

And this gives:

stdClass Object
(
    [country_name] => NETHERLANDS
    [country_code] => NL
    [city] => (Unknown city)
    [ip] => xx.xx.10.9
)

This can be used as:

echo $ipinfo->city;

Need Your Help

how can i read data from csv file and store in binary tree and write multiple file in perl

perl csv binary-tree

I have CSV file which have more than 10 lakh data. I want to use binary::tree for less memory uses.

Optimize multi-join query, please

mysql sql query-optimization average

i made a query for sql but it's really heavy and i really don't see how i can optimize it, the explanations comes after :