PDA

View Full Version : PHP : How do you replace last # in IP by xxx?


Moonlight
01-25-2000, 07:10 PM
I would like, in PHP, to be able to replace the last subnet of the IP address by "xxx".

For example :

123.456.789.000

to become :

123.456.789.xxx?

How do I do it?

Moonlight

Charles Capps
01-25-2000, 09:31 PM
Try:

ereg("([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)", $REMOTE_ADDR, $address);
$ipmask = "$address[1].$address[2].$address[3].xxx";

$REMOTE_ADDR is the IP you wish to split, $address is the array into which the IP is split, then $ipmask is set to contain the first three bits of the IP, leaving a dot at the end.
[This message has been edited by Charles Capps (edited 01-25-00@8:32 pm)]

Justin
01-25-2000, 10:36 PM
How about a one liner with no array?
</font><font face="Courier" size="3">
$IP = ereg_replace (&quot;^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)[0-9]{1,3}$&quot;, &quot;\\1xxx&quot;, $IP);
</font><font face="Verdana, Arial" size="2">
Hope this helps.

------------------
Justin Nelson
FutureQuest Support