Reputation: 1709
If I have an IP such as:
195.123.321.456
How do I get just the 456
as a variable?
Upvotes: 1
Views: 2675
Reputation: 21
You can find this the following way:
$ipaddress = '195.123.321.456';
$endValue = end( explode(".", $ipaddress ) );
echo $endValue;
Upvotes: 1
Reputation: 63
U could use also this:
$ip = '195.123.321.456';
$last = substr($ip, strrpos($ip, '.') + 1);
Upvotes: 0
Reputation: 582
If you store it inside a Var like
$var = "195.123.321.456";
you can use the preposed php command on string to find the last occurence.
$number = substr(strrchr($var , "."), 1);
You will have now 456 on $number var
Documentation on strrchr -> http://php.net/manual/en/function.strrchr.php
Upvotes: 1
Reputation: 59701
This should work for you:
<?php
$ip = "195.123.321.456";
$split = explode(".", $ip);
echo $split[3];
?>
Output:
456
Upvotes: 9
Reputation: 67978
\b\d+(?![^.]*\.)
Try this.See demo.
http://regex101.com/r/pQ9bV3/22
$re = "/\\b\\d+(?![^.]*\\.)/";
$str = "195.123.321.456";
preg_match_all($re, $str, $matches);
Upvotes: 1