Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show part of a IP address

85.124.99.2

How can I hide the last two numbers from the IP?

and make it like:

86.124.xxx.xxx

like image 424
ILikeTurtles Avatar asked Dec 13 '22 11:12

ILikeTurtles


2 Answers

Wrote this quickly

$ip = "85.124.99.2";
$parts = explode('.',$ip);

$new_ip = $parts[0].'.'.$parts[1].'.xxx.xxx';

Warning: You should test the length of parts before accessing $parts[n]

like image 82
Eddie Avatar answered Dec 27 '22 14:12

Eddie


$ip = preg_replace('/\.\d+\.\d+$/', '.xxx.xxx', $ip);
like image 24
Karolis Avatar answered Dec 27 '22 13:12

Karolis