View Full Version : using string functions

07-14-2008, 10:14 AM
i am using $ip= $_SERVER['REMOTE_ADDR'] to retrieve the ip address of the client for example if the value returned from $ip


i would like to find out which country the request has come from. i believe by using the third set of numbers (in this

case=190) from an ip address we can find out the country name. i can declare a variable with a list of country names and the

range of values, what i need help is to extract the 3rd set of numbers from $ip.

as ip address keep changing the 3rd set of numbers can be single, double or 3 digit number how can we extract the number that

is stored in $ip after the second dot and before the third dot

please advice.


07-14-2008, 10:21 AM
Using explode will allow you to split it into pieces:

$ip = '';
$aIP = explode('.', $ip);
/* IP Now contains:
$aIP[0] = 50;
$aIP[1] = 160;
$aIP[2] = 190;
$aIP[3] = 150;

You'll be in for a surprise if you think that the third octlet is relational to the country though...

07-14-2008, 04:07 PM
Its usually the first one.