Re: Removing an aspect of a variable...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux