Getting A Variable out of an array?

Discussion in 'PHP' started by medialab, Oct 31, 2013.

  1. #1
    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: (.*?)&nbsp;#i';
                  $patterns["country"] = '#Country: (.*?)&nbsp;#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:
     
    Solved! View solution.
    medialab, Oct 31, 2013 IP
  2. #2
    
    $ip = geoCheckIP($ip);
    
    echo $ip['state'];
    echo $ip['town'];
    
    PHP:
    ... ?
     
    nico_swd, Oct 31, 2013 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #3
    Hey Nico,

    Thanks for your reply, when I echo state though, it just says 2?


     
    medialab, Oct 31, 2013 IP
  4. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #4
    Nevermind, I had it in the wrong spot! Thanks!!
     
    medialab, Oct 31, 2013 IP
  5. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #5
    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.
    :)


    .
     
    ROOFIS, Oct 31, 2013 IP