PHP numbers to words SPANISH!
Asked Answered
S

4

5

Does anyone know of a free-licence PHP code that would convert numbers to words in spanish?
It's needed for my work for generating bills so it needs to be accurate.
My knowledge of spanish is practically non-existent, so it would probably be hard to write it myself; I don't know anything about spanish grammar.

Edit: I've written my own version of this, for now it works for only 3 digit numbers (on both sides of the decimal symbol), but it's a good start. If I ever get it big (5 languages planned atm), I'll probably share it on github. Don't bet on it though.

Sharika answered 12/7, 2011 at 7:6 Comment(5)
Do you mean translate a literal number into the Spanish word for that number?Willi
@Sharika A code-example would be very helpful. Are you trying to do something along the lines of echo translate(100); // ciento?Intramolecular
@Jonathan - Yes, something like that, correctly spelled numbers in spanish; not just separate digits, the whole number.Sharika
Apart from the perl answer by @bazmegakapa, take a look at this comment on php's number_format manual page, to translate into English.Ankylose
But will that be GRAMMATICALLY correct?Sharika
B
0

here's the words... 1 to 100 http://www.top-tour-of-spain.com/1-100-Numbers-In-Spanish.html

100+

http://www.intro2spanish.com/vocabulary/numbers/advanced.htm

or

http://www.donquijote.org/spanishlanguage/numbers/numbers2.asp

and I found some nicely formatted VBA here:

http://www.excelforum.com/excel-programming/637231-translating-numbers-into-words.html

Bolzano answered 12/7, 2011 at 7:23 Comment(3)
VBA? the question has been tagged with 'php'Hellhound
Ok, that helps a lot, I'll try to write my own converter with that (Not the VBA code) :)Sharika
@Jamie, the first 2 links show the language rules (for reference) and the final link (note i did say it was VBA) was included as it show the rules as well formatted code, that could easily be translated to PHP.Bolzano
C
11

You may use PHP Intl extension to do that.

if (version_compare(PHP_VERSION, '5.3.0', '<')
    || !class_exists('NumberFormatter')) {
    exit('You need PHP 5.3 or above, and php_intl extension');
}

$formatter = new \NumberFormatter('es', \NumberFormatter::SPELLOUT);
echo $formatter->format(1234567) . "\n";

Output:

un millón doscientos treinta y cuatro mil quinientos sesenta y siete

It works not only with Spanish but many other languages as well.

Cautious answered 2/8, 2013 at 8:38 Comment(0)
H
3

Is there an easy way to convert a number to a word in PHP?

From the above, you can derive the "word" from the number, and then translate it to any language you like using any of the Translate API's out there ... or your own lookups.

Edit

Another way you could employ is simply hard coding in a PHP file or a text file a big array of values:

 $numTranslation = array(1 => "uno", 2 => "dos");

Then, in your code, just retrieve the echo $numTranslation[2] if the number was 2 to print out the spanish equivalent.

Edit 2

Just to make it a bit more complete, if you ever want to support multiple languages, and not just spanish:

 $numTranslation = array(
   1 => array("en" => "One", "es" => "uno"), 
   2 => array("en" => "Two", "es" => "dos")
 );

And to print it out to the end user: echo $numTranslation[2]['es']; to get the Spanish equivalent...

Hyperbolic answered 12/7, 2011 at 7:17 Comment(4)
Are you sure that will be correct? That would be DIRECT translation, same as using Google translator, and you know it could end badly. I already have an english number2words code, but translation is not a good idea.Sharika
If you have implicit faith in translation API's, go for it...if you want to be absolutely sure, you need to maintain your own dictionary for the translations ... or create a simple 1:1 lookup between number and word in an array or text fileHyperbolic
@jurchiks: I think if you are only translating a number using e.g. Google translate, then you will be fine. Numbers are concrete and unambiguous, so easy for automatic translation software. Especially since the target language is Spanish, which is a much more regular language than most.Jolinejoliotcurie
That is the basic structure of any language converter, but the grammar differences make it much harder than just that, unless you're really thinking of just writing out every number up to millions and higher. The main idea between every converter is to make out some kind of algorithm to make a reasonable string of words while still maintaining correct grammar, and to make it all using as little code as possible.Sharika
B
1

The only thing I could find is a Perl script.

The code itself is easy to write in PHP, and from the Perl script you can get the logic and the Spanish words for numbers.

Bracy answered 12/7, 2011 at 7:14 Comment(1)
I'm totally not familiar with Perl; tried reading it and it is hard to understand. It would take me hours to get the logic correctly... Also, that code is really messy; so many ELSE's and not optimized at all...Sharika
B
0

here's the words... 1 to 100 http://www.top-tour-of-spain.com/1-100-Numbers-In-Spanish.html

100+

http://www.intro2spanish.com/vocabulary/numbers/advanced.htm

or

http://www.donquijote.org/spanishlanguage/numbers/numbers2.asp

and I found some nicely formatted VBA here:

http://www.excelforum.com/excel-programming/637231-translating-numbers-into-words.html

Bolzano answered 12/7, 2011 at 7:23 Comment(3)
VBA? the question has been tagged with 'php'Hellhound
Ok, that helps a lot, I'll try to write my own converter with that (Not the VBA code) :)Sharika
@Jamie, the first 2 links show the language rules (for reference) and the final link (note i did say it was VBA) was included as it show the rules as well formatted code, that could easily be translated to PHP.Bolzano

© 2022 - 2024 — McMap. All rights reserved.