Results 1 to 10 of 10

Thread: How to split this information? (code included)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Exclamation How to split this information? (code included)

    I'm working with GEO IP, I can display the information, please see the below code and notes.

    Code:
    <?PHP
    
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    
    $license_key = "PRIVATE";
    
    $query = "http://maxmind.com:8010/f?l=" . $license_key . "&i=" . $ipaddress;
    $url = parse_url($query);
    $host = $url["host"];
    $path = $url["path"] . "?" . $url["query"];
    $timeout = 1;
    $fp = fsockopen ($host, 8010, $errno, $errstr, $timeout)
    	or die('Can not open connection to server.');
    if ($fp) {
      fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
      while (!feof($fp)) {
        $buf .= fgets($fp, 128);
      }
      $lines = split("\n", $buf);
      $data = $lines[count($lines)-1];
      fclose($fp);
    } else {
      //echo "uh oh";
    }
    echo $data;
    ?>

    The code outputs this information for me:

    US,NY,Ithaca,,42.427799,-76.498199,555,607,"Road Runner","Road Runner"



    I would ONLY like to display the "607", US, NY, Ithaca in different places on my page.

    So If someone could help me split this information that would be great.

    On my page I want to display the information as:

    Ithaca, NY - US

    and 607 (area code) will be displayed in a completely different location so I would like to be able to put this code anywhere on my site to display the area code:

    Code:
    <?PHP echo $areacode; ?>
    In fact I'd like to do that with all information.

    Code:
    <?PHP echo $city; ?>
    Code:
    <?PHP echo $country; ?>

    thank you!!

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to split this information? (code included)

    There's an explode() function in PHP. Look it up.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Re: How to split this information? (code included)

    Okay so I'm this far:
    I have very little knowledge of PHP and dont know how i would turn the data into separate variables.


    Code:
    <?PHP
    
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    
    $license_key = "PRIVATE";
    
    $query = "http://maxmind.com:8010/f?l=" . $license_key . "&i=" . $ipaddress;
    $url = parse_url($query);
    $host = $url["host"];
    $path = $url["path"] . "?" . $url["query"];
    $timeout = 1;
    $fp = fsockopen ($host, 8010, $errno, $errstr, $timeout)
    	or die('Can not open connection to server.');
    if ($fp) {
      fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
      while (!feof($fp)) {
        $buf .= fgets($fp, 128);
      }
      $lines = split("\n", $buf);
      $data = $lines[count($lines)-1];
      fclose($fp);
    } else {
      //echo "uh oh";
    }
    echo $data;
    
    ?>
    
    
    <?PHP
    
    $str = $data;
    
    // positive limit
    print_r(explode(',', $str, 2));
    
    // negative limit (since PHP 5.1)
    print_r(explode(',', $str, -1));
    ?>

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to split this information? (code included)

    Array indexing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to split this information? (code included)

    why are you using limits? didn't you read the documentation for the function?

    PHP Code:
    <?php
      $data 
    'US,NY,Ithaca,,42.427799,-76.498199,555,607,"Road Runner","Road Runner"';
      list(
    $country$state$city$junk[], $junk[], $junk[], $junk[], $areacode) = explode(","$data);
    ?>
    you live in <?php echo "{$city}{$state}{$country}"?>. your area code is <?php echo $areacode?>.
    <?php print_r($junk); ?>

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Re: How to split this information? (code included)

    Quote Originally Posted by kows
    why are you using limits? didn't you read the documentation for the function?

    PHP Code:
    <?php
      $data 
    'US,NY,Ithaca,,42.427799,-76.498199,555,607,"Road Runner","Road Runner"';
      list(
    $country$state$city$junk[], $junk[], $junk[], $junk[], $areacode) = explode(","$data);
    ?>
    you live in <?php echo "{$city}{$state}{$country}"?>. your area code is <?php echo $areacode?>.
    <?php print_r($junk); ?>

    OK AWESOME.

    Just one more thing -

    When i use your code it shows:

    US,NY,Ithaca,,42.427799,-76.498199,555,607,"Road Runner","Road Runner" you live in Ithaca, NY, US. your area code is 607.


    I would like it to only display:
    you live in Ithaca, NY, US. your area code is 607.



    Then I'm done.

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to split this information? (code included)

    uhh... then don't echo out $data? nothing in my code even echoes that out in the first place.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Re: How to split this information? (code included)

    Quote Originally Posted by kows
    uhh... then don't echo out $data? nothing in my code even echoes that out in the first place.

    Ohhh sorry.


    Forgive me for not knowing PHP. If i knew everything about php I wouldn't be posting on these forums.


    Thanks either way.
    8 gigs/ram (hey why not)
    300 gig HD x2
    Windows XP 64

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to split this information? (code included)

    Nobody knows everything about PHP. However, not noticing that you print a variable is ... far from knowing everything.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: How to split this information? (code included)

    Quote Originally Posted by erbio
    Ohhh sorry.


    Forgive me for not knowing PHP. If i knew everything about php I wouldn't be posting on these forums.


    Thanks either way.
    echo "hello world" is right after you learn about
    Code:
    <?php ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width