Re: Removing an aspect of a variable...

From: Date: Tue, 06 Jun 2006 08:08:26 +0000
Subject: Re: Removing an aspect of a variable...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-237447@lists.php.net to get a copy of this message
At 12:37 AM 6/6/2006, Rob W. wrote:
Say I have a variable setting an ip address of 192.168.100.0 I want to be able to remove the last to chr's of that variable ie: .0 What would be my best solution to do that?
If you want the last two characters you can use substr():
[1]     $sResult = substr($sIP, -2);
If you want the string from the last period onward, regardless of length, you can use strrpos() and substr():
[2]     $iPos = strrpos($sIP, ".");
                if ($iPos !== FALSE) $sResult = substr($sIP, -$iPos);
Take the time to read about various string functions -- you'll thank yourself. http://php.net/strings Paul [1] substr http://php.net/substr see Example 2. Using a negative start [2] strrpos http://php.net/strrpos

« previous php.general (#237447) next »