class NumberParser (View source)

Parser for numbers.

This parser does not support full syntax of number formats as defined in CLDR. It uses parsed formats from NumbersReader class.

Currency support

Constants

PATTERN_MATCH_DIGITS

Regex pattern for matching one or more digits.

PATTERN_MATCH_NOT_DIGITS

Regex pattern for matching all except digits. It's used for clearing string in lenient mode.

Properties

protected NumbersReader $numbersReader

Methods

void
injectNumbersReader(NumbersReader $numbersReader)

No description

mixed
parseNumberWithCustomPattern(string $numberToParse, string $format, Locale $locale, bool $strictMode = true)

Parses number given as a string using provided format.

mixed
parseDecimalNumber(string $numberToParse, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses decimal number using proper format from CLDR.

mixed
parsePercentNumber(string $numberToParse, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses percent number using proper format from CLDR.

mixed
doParsingWithParsedFormat(string $numberToParse, array $parsedFormat, array $localizedSymbols, bool $strictMode)

Parses number using parsed format, in strict or lenient mode.

mixed
doParsingInStrictMode(string $numberToParse, array $parsedFormat, array $localizedSymbols)

Parses number in strict mode.

mixed
doParsingInLenientMode(string $numberToParse, array $parsedFormat, array $localizedSymbols)

Parses number in lenient mode.

Details

void injectNumbersReader(NumbersReader $numbersReader)

No description

Parameters

NumbersReader $numbersReader

Return Value

void

mixed parseNumberWithCustomPattern(string $numberToParse, string $format, Locale $locale, bool $strictMode = true)

Parses number given as a string using provided format.

Parameters

string $numberToParse

Number to be parsed

string $format

Number format to use

Locale $locale

Locale to use

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Parsed float number or false on failure

mixed parseDecimalNumber(string $numberToParse, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses decimal number using proper format from CLDR.

Parameters

string $numberToParse

Number to be parsed

Locale $locale

Locale to use

string $formatLength

One of NumbersReader FORMAT_LENGTH constants

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Parsed float number or false on failure

mixed parsePercentNumber(string $numberToParse, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses percent number using proper format from CLDR.

Parameters

string $numberToParse

Number to be parsed

Locale $locale

Locale to use

string $formatLength

One of NumbersReader FORMAT_LENGTH constants

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Parsed float number or false on failure

protected mixed doParsingWithParsedFormat(string $numberToParse, array $parsedFormat, array $localizedSymbols, bool $strictMode)

Parses number using parsed format, in strict or lenient mode.

Parameters

string $numberToParse

Number to be parsed

array $parsedFormat

Parsed format (from NumbersReader)

array $localizedSymbols

An array with symbols to use

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Parsed float number or false on failure

protected mixed doParsingInStrictMode(string $numberToParse, array $parsedFormat, array $localizedSymbols)

Parses number in strict mode.

In strict mode parser checks all constraints of provided parsed format, and if any of them is not fullfiled, parsing fails (false is returned).

Parameters

string $numberToParse

Number to be parsed

array $parsedFormat

Parsed format (from NumbersReader)

array $localizedSymbols

An array with symbols to use

Return Value

mixed

Parsed float number or false on failure

protected mixed doParsingInLenientMode(string $numberToParse, array $parsedFormat, array $localizedSymbols)

Parses number in lenient mode.

Lenient parsing ignores everything that can be ignored, and tries to extract number from the string, even if it's not well formed.

Implementation is simple but should work more often than strict parsing.

Algorithm:

  1. Find first digit
  2. Find last digit
  3. Find decimal separator between first and last digit (if any)
  4. Remove non-digits from integer part
  5. Remove non-digits from decimal part (optional)
  6. Try to match negative prefix before first digit
  7. Try to match negative suffix after last digit

Parameters

string $numberToParse

Number to be parsed

array $parsedFormat

Parsed format (from NumbersReader)

array $localizedSymbols

An array with symbols to use

Return Value

mixed

Parsed float number or false on failure