Re: Integer
| From: | tamouse mailing lists | Date: | Sat, 02 Feb 2013 05:27:04 +0000 |
| Subject: | Re: Integer | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320143@lists.php.net to get a copy of this message | ||
On Fri, Feb 1, 2013 at 10:40 PM, Ron Piggott
<ron.piggott@actsministries.org> 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"
}