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 today

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

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

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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