ILikeTurtles
ILikeTurtles

Reputation: 43

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

Upvotes: 4

Views: 945

Answers (4)

Eddie
Eddie

Reputation: 13126

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]

Upvotes: 11

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46425

Use this regex.

(?!\d{1,3}\.\d{1,3}\.)\d

Sample

137.133.204.130 -> 137.133.***.***  
93.108.72.157 -> 93.108.**.***

Upvotes: 0

johnny
johnny

Reputation: 19755

Consume the IP Address and only print out what you want. Not sure other than that what you mean by "make it" other than printing on the screen.

Upvotes: 0

Karolis
Karolis

Reputation: 9582

$ip = preg_replace('/\.\d+\.\d+$/', '.xxx.xxx', $ip);

Upvotes: 2

Related Questions