Re: Problems converting strings with 0 to integer
| From: | Alexander Holodny | Date: | Wed, 03 Nov 2010 21:42:53 +0000 |
| Subject: | Re: Problems converting strings with 0 to integer | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-309281@lists.php.net to get a copy of this message | ||
To exclude unexcepted behavior in case of wrongly formated input data,
it would be much better to use such type-casting method:
intval(ltrim(trim($inStr), '0'))
2010/11/3, Nicholas Kell <nick@monkeyknight.com>:
>
> On Nov 3, 2010, at 4:22 PM, robert mena wrote:
>
>> Hi,
>>
>> I have a text file (utf-8 encoded) which contains lines with numbers and
>> text separated by \t. I need to convert the numbers that contains 0 (at
>> left) to integers.
>>
>> For some reason one line that contains 00000002 is casted to 0 instead of
>> 2.
>> Bellow the output of the cast (int) $field[0] where I get this from
>> explode each line.
>>
>> 0 00000002
>> 4 00000004
>
>
>
> My first guess is wondering how you are grabbing the strings from the file.
> Seems to me like it would just drop the zeros on the left by default. Are
> you including the \t in the string by accident? If so, that may be hosing
> it. Otherwise, have you tried ltrim on it?
>
> Ex:
>
> $_castableString = ltrim($_yourString, '0');
>
> // Now cast
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>