Hey Everyone, I am new to PHP and learning a lot but this one has me stumped, I am at variables in an array, and I for the life of me don't understand what I am doing wrong, searched for hours! I am trying to pull 2 variables out of this array, city and state, and display them on a php page. Any help would be greatly appreciated. I know they are there, because the array is displaying on the page, but I don't want to display the whole thing, just the 2 variables. <?php $ip=$_SERVER['REMOTE_ADDR']; print_r(geoCheckIP($ip)); //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen ) //Get an array with geoip-infodata function geoCheckIP($ip) { //check, if the provided ip is valid if(!filter_var($ip, FILTER_VALIDATE_IP)) { throw new InvalidArgumentException("IP is not valid"); } //contact ip-server $response=@file_get_contents('http://www.netip.de/search?query='.$ip); if (empty($response)) { throw new InvalidArgumentException("Error contacting Geo-IP-Server"); } //Array containing all regex-patterns necessary to extract ip-geoinfo from page $patterns=array(); $patterns["domain"] = '#Domain: (.*?) #i'; $patterns["country"] = '#Country: (.*?) #i'; $patterns["state"] = '#State/Region: (.*?)<br#i'; $patterns["town"] = '#City: (.*?)<br#i'; //Array where results will be stored $ipInfo=array(); //check response from ipserver for above patterns foreach ($patterns as $key => $pattern) { //store the result in array $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found'; } return $ipInfo; } ?> PHP:
A couple of handy hints, you could of echo'ed the var's from the array like so, $output = geoCheckIP($ip); echo $output["state"]; echo $output["town"]; PHP: Or better yet to save some of that regex overhead modify the function itself. remove or hash out lines 24 and 25 since your not wanting to seek $patterns["domains"] and $patterns["country"] and modify line 39 return $ipInfo; to either return implode(" ",$ipInfo); return implode(PHP_EOL,$ipInfo); PHP: " ", for side by side listing or PHP_EOL, for vertical listing. Then for call to function simply: echo geoCheckIP($ip); To change output order swap around line 26 $patterns["state"] and line 27 $patterns["town"] if you wanted to render City before State on the output page. .