Re: Removing an aspect of a variable...

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

 



On Tue, 6 Jun 2006 08:52:46 +0100, Robin Vickery wrote:

>On 06/06/06, Rob W. <rob@xxxxxxxxxxxxxxx> 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?
>>
>
>Remove the last two characters of a string?
>
>   $shorterString = substr($string, 0, -2);
>
>Whether that's really what you want to be doing with an IP address is up to you.

substr will remove the last two characters from a string, as mentioned
above.

If you want to remove the last byte from an IP address (which could be
.0, .10, or .100) I would suggest:

$ip = "192.168.100.0";
$ip = explode ('.', $ip);
array_pop ($ip);
$ip = implode ('.', $ip);

With PHP 5.1 and up, explode accepts a negative limit, which would
simplify things.

-- 

http://www.otton.org/

-- 
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