Skip to content

Useful PHP functions for converting between numbers and text

License

Notifications You must be signed in to change notification settings

lukesrw/reverse-spellout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

reverse-spellout

Functions

numberToText

Wrapper for the NumberFormatter class (removes hyphens, commas, include various 'and').

textToNumber

Converts text (from numberToText) back into numbers, handles any size number.

Usage

You can use these functions to convert between numbers and text:

<?php

$number = 1234567890;

echo $number . "\n\n";

echo numberToText($number) . "\n\n";

echo textToNumber(numberToText($number));

The above code will output:

1234567890

one billion, two hundred and thirty-four million, five hundred and sixty-seven thousand, eight hundred and ninety

1234567890

Options

You can pass an array of options to the numberToText function as the second argument.

If you pass only some options in the array, or no array at all - the default values from the table below are used.

<?php

$number = 1234567890;

echo numberToText(
    $number,
    array(
        'insert_ands' => 'one'
    )
);
Option Description Values Default
locale Locale used for \NumberFormatter IANA Language Subtags Registry 'en'
insert_ands Number of "and" segments to include 'one', 'many', false 'many'
insert_commas Whether to include commas after thousand/-illian tokens true, false true
remove_hyphens Remove hyphens from text (i.e. "thirty-four") true, false false

About

Useful PHP functions for converting between numbers and text

Topics

Resources

License

Stars

Watchers

Forks

Languages