Browse project documentation
Words to number
Parse Persian number words into integers or floats.
Eram\Abzar\Format\WordsToNumber parses Persian number words back to int or float. It complements Eram\Abzar\Format\NumberToWords::convert(), but floating-point round trips are not guaranteed to preserve every value.
Minimal example
<?php
require 'vendor/autoload.php';
use Eram\Abzar\Format\WordsToNumber;
var_dump(WordsToNumber::parse('یک هزار و دویست و سی و چهار'));
var_dump(WordsToNumber::parse('سه ممیز پنج'));
var_dump(WordsToNumber::parse('دو سه'));
int(1234)
float(3.5)
NULL
More examples
use Eram\Abzar\Format\WordsToNumber;
WordsToNumber::parse('یک هزار و دویست و سی و چهار'); // 1234
WordsToNumber::parse('منفی پنج'); // -5
WordsToNumber::parse('سه ممیز پنج'); // 3.5 (float)
WordsToNumber::parse('foo bar'); // null — unparseable
WordsToNumber::parse('دو سه'); // null — not a number
WordsToNumber::parse('سه صد'); // 300 — split hundreds
WordsToNumber::parse('هزار میلیارد'); // 1000000000000
Rules
- Leading
منفیflips the sign. ممیزswitches to fractional mode — the post-separator integer is divided by10^digitsand the result becomes afloat.- Leading
یکbeforeهزار/میلیون/ etc. is optional. - Mixed word + digit input (e.g.
یک هزار و 200) returnsnull. Normalize to pure words or pure digits first. - Whitespace and ZWNJ separate tokens; the
وconjunction is treated as a separator and is optional (بیست دو= 22). - Within each group below a thousand, the words must step down in size: hundreds, then tens, then ones, or a single teen. Sequences such as
دو سه,بیست سیorیازده دوreturnnullinstead of being summed. A ones word followed byصد(سه صد,یک صد) is read as split hundreds. هزارmultiplies the group in front of it, once per group. Larger scales (میلیونand up) must appear in decreasing order, soهزار میلیاردis accepted butیک میلیون دو میلیونreturnsnull.- Besides the forms
NumberToWordswrites, these alternate spellings are accepted:شیش(6),صد(100),چارصد(400),بیلیون(10⁹, the same scale asمیلیارد) andکوآدریلیون(10¹⁵). Together with split hundreds, this covers everything persian-tools’numberToWords()writes. See persian-tools parity.
Precision ceiling
Integer results fit in int up to PHP_INT_MAX (≈ 9.2 × 10¹⁸ on 64-bit PHP; lower on 32-bit PHP). Larger values (e.g. ده کوینتیلیون) return null. If you need big-integer semantics, use a dedicated math library.
Common mistakes
Do not coerce null to zero: it means parsing failed. Currency suffixes, ordinal words, fuzzy prose and mixed digits/words are not supported. Fractional results are PHP floats, not exact money values. Use integer rials with Amount.
Related: formatting, digits, Persian text.