PHPackages                             gugglegum/number-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. gugglegum/number-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

gugglegum/number-parser
=======================

1.0.0(3y ago)210MITPHPPHP &gt;=8.0

Since Mar 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/gugglegum/number-parser)[ Packagist](https://packagist.org/packages/gugglegum/number-parser)[ RSS](/packages/gugglegum-number-parser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Number Parser
=============

[](#number-parser)

This package does one very simple thing: it converts strings containing decimal numbers into numbers (int or float). But it does this by checking if the number in the original string is valid and throwing an exception if something is wrong.

Why would it be useful?
-----------------------

[](#why-would-it-be-useful)

There are many methods in PHP to convert a string to a number. To convert an integer you can use:

```
$i = (int) $str;
$i = intval($str);
```

For fractional you can use:

```
$f = (float) $str;
$f = floatval($str);
```

But the problem with these methods is that they continue to work even if the string was not quite a number, or not even a number at all. For example:

```
var_dump(intval('a')); // prints int(0)

var_dump(intval(new stdClass())); // prints int(1) (although with a PHP Warning "Object of class stdClass could not be converted to int")
```

In error-sensitive projects, where mistakes often result in a loss of money, you may want to use the strictest possible validation of all input data. In that case, you may want to try using standard methods:

```
$n = filter_var($str, FILTER_VALIDATE_INT);
$n = filter_var($str, FILTER_VALIDATE_FLOAT);
```

Now, if `$str` contains anything other than a number (for example, `"123a"`), then `$n` will contain `FALSE`. However, if you are building your error handling logic around exceptions, you need to add a code like this:

```
if ($n === false) {
    throw new Exception("Invalid number in string");
}
```

Now imagine that you need to do this in multiple places! However, while `filter_var()` is good and has many options, its validators are sometimes not strict enough. For example, `filter_var()` allows numbers with spaces at the beginning or at the end (`" 123 "`), allows a minus sign before a zero (`"-0"`). Also, you may not always want to see numbers in exponential form as input (`"1.234e-4"`).

How to use it?
==============

[](#how-to-use-it)

It's very easy:

```
composer require gugglegum/number-parser

```

```
use \gugglegum\NumberParser\NumberParser;

try {
    ...

    $i = NumberParser::parseInt($str1);
    $f = NumberParser::parseFloat($str2);
    $e = NumberParser::parseFloatExponent($str3);

    ...
} catch (Exception $e) {
    echo "Error: {$e->getMessage()}\n";
}
```

It makes sense to apply these methods to all numerical data obtained from the outside: `$_SERVER['argv']`, `$_GET`, `$_POST`, `$_COOKIE`, `$_ENV`, `fgets()`, etc.

### Examples of valid numbers

[](#examples-of-valid-numbers)

Integers:

```
"1"
"-12345"
"1234567890"

```

Fractional (float):

```
"0.1"
"0.10"
".1"
"1.23"

```

Fractional with exponent:

```
"1e2"
"1e+2""
"1e-2"
"1.23e2"
"12.3e1"
"0.1e2"
".123e-2"

```

### Examples of invalid numbers

[](#examples-of-invalid-numbers)

Integers:

```
"a567"
"567a"
" 567"
"567 "
"56_7"
"-0"
"00"
"-00"
"01"
"+1"
".1"
"1e2"
""

```

Fractional (float):

```
"a56.7"
"56.7a"
" 56.7"
"56.7 "
"5_6.7"
"00"
"00.0"
"0"
"-0.0"
"-.0"
"+1.2"
"."
""

```

Fractional with exponent:

```
"e",
"1e",
"1e+",
"1e-",
"e+2",
"e-2",
".e2",

```

How do I run the tests?
-----------------------

[](#how-do-i-run-the-tests)

```
composer intall
vendor/bin/phpunit

```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1164d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8c7fbb933ba217a413e632183e6061f81bd1d285bb86efa82289977038577d88?d=identicon)[gugglegum](/maintainers/gugglegum)

---

Top Contributors

[![gugglegum](https://avatars.githubusercontent.com/u/1580712?v=4)](https://github.com/gugglegum "gugglegum (3 commits)")

---

Tags

php

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gugglegum-number-parser/health.svg)

```
[![Health](https://phpackages.com/badges/gugglegum-number-parser/health.svg)](https://phpackages.com/packages/gugglegum-number-parser)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
