Re: Integer

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

 



On Fri, Feb 1, 2013 at 10:40 PM, Ron Piggott
<ron.piggott@xxxxxxxxxxxxxxxxxx> wrote:
> In the following the “2.” means a moderator response and “25” is the account # of the moderator.
>
> <?php
>
> $author = 2.0000000000000000000000025
>
> ?>
>
> How can I get the 25 by itself?
> - I want to drop the “2.” and remove all the zero’s
>
> Would it be best to turn this into a STRING?
>
> Any suggestions?

Storing data as a float is a bad idea. Way bad. Also, I think that's
too many significant digits for php to handle in a float.

You should store data in an accessible way. For instance, assuming
there are actually only two pieces of interesting data there, you
could store them as a hash:

php > $author = Array('account' => 25, 'response' => 2);
php > var_dump($author);
array(2) {
  ["account"]=>
  int(25)
  ["response"]=>
  int(2)
}

If that data is coming from somewhere beyond your control and is
presented as that, then parsing as a string will be the way to go.
Again, assuming there is no other useful information, a simple regexp
to parse it would be:

php > if (preg_match('/(\d+)\.0+(\d+)/','2.0000000000000000000000025',$matches))
{
php {   $author = Array('account' => $matches[2], 'response' => $matches[1]);
php { }
php > // else handle error if need be
php > var_dump($author);
array(2) {
  ["account"]=>
  string(2) "25"
  ["response"]=>
  string(1) "2"
}

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