|
-
Feb 15th, 2007, 10:11 AM
#1
Thread Starter
Hyperactive Member
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!!
-
Feb 15th, 2007, 10:22 AM
#2
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.
-
Feb 15th, 2007, 10:33 AM
#3
Thread Starter
Hyperactive Member
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));
?>
-
Feb 15th, 2007, 10:52 AM
#4
Re: How to split this information? (code included)
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.
-
Feb 15th, 2007, 11:04 AM
#5
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); ?>
-
Feb 15th, 2007, 11:13 AM
#6
Thread Starter
Hyperactive Member
Re: How to split this information? (code included)
 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.
-
Feb 15th, 2007, 11:18 AM
#7
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.
-
Feb 15th, 2007, 11:24 AM
#8
Thread Starter
Hyperactive Member
Re: How to split this information? (code included)
 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
-
Feb 15th, 2007, 11:34 AM
#9
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.
-
Feb 17th, 2007, 01:49 AM
#10
Hyperactive Member
Re: How to split this information? (code included)
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|