tags:

views:

100

answers:

2

Does anyone know how can I get the last digit number from the Ip address in php?

example:

$ip = '200.0.0.12';

How can I get only 12 from the IP address instead of 200.0.0.12?

+5  A: 

Assuming the ip is well-formed (no ports, ipv4, etc)

$last_digit = array_pop(explode('.', $ip))
Mike B
+1  A: 

I like Mike B's answer, but here's a possible alternative: use strrchr and substr:

$ip = '200.0.0.12';
echo substr(strrchr($ip,'.'),1);

One advantage: it's supposed to be (a little) faster then Mike B's answer.

In my (very unscientific) timings i got an average runtime of 1.3562 seconds (500.000 iterations) versus 1.6590 seconds for the array_pop / explode version.

ChristopheD