Frage

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.

War es hilfreich?

Lösung

Use explode,

$nums = explode(".", "192.168.1.6") ;
echo $nums[0]. "." .$nums[1]; //192.168

Andere Tipps

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];

Hi you can do like this:

substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'], '.',strpos($_SERVER['REMOTE_ADDR'], '.')));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top