Reputation: 9
I wanna show two first range of an IP. For example, I have 127.0.0.1. I wanna get 127.0 and use this example:
127.0.0.1
show 127.0
192.168.1.6
show 192.168
How can I do that?
I tried $_SERVER['REMOTE_ADDR']
but it shows the whole IP address.
Upvotes: 0
Views: 127
Reputation: 49
Hi you can do like this:
substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'], '.',strpos($_SERVER['REMOTE_ADDR'], '.')));
Upvotes: 0
Reputation: 1989
Use preg_split
$your_ip = ...
$split = preg_split ("/./",$your_ip,NULL);
$your_new_ip = $split[0].".".$split[1];
Or explode
$your_ip = ...
$split = explode(".", $your_ip);
$your_new_ip = $split[0].".".$split[1];
Upvotes: 2