Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- */
- // Staring straight up into the sky ... oh my my
- error_reporting(-1);
- mb_internal_encoding('utf-8');
- /* Возвращает соответствующую числу форму слова: 1 рубль, 2 рубля, 5 рублей */
- function inclineWord($number, $word1, $word2, $word5) {
- /* DIY */
- }
- //функция выбора формы слова
- function getWordForm($number, $order){
- $formsRub = array("рубль","рубля","рублей");
- $formsThousand = array("тысяча","тысячи","тысяч");
- $formsMill = array("миллион","миллиона","миллионов");
- $forms = array($formsRub, $formsThousand, $formsMill);
- if($number > 99){
- $number-=floor($number/100)*100;
- }
- if (($number > 4) and ($number < 21)) {
- return $forms[$order][2];
- }
- $num = floor($number%10);
- if($num == 1){
- return $forms[$order][0];
- }
- if((($num > 1 )and($num < 5))){
- return $forms[$order][1];
- }
- if(($num > 4 )and($num < 11)or($num == 0)){
- return $forms[$order][2];
- }
- }
- //принимает на вход число от 0 до 999 и возвращает его текстовую форму
- function spellSmallNumber($number, $isFemale){
- $num = $number;
- $words = array();
- if($num==0){
- array_push($words, smallNumberToText($num, $isFemale));
- return implode($words," ");
- }
- if ($num > 99){
- $order = floor($num/100)*100;
- array_push($words, smallNumberToText($order, $isFemale));
- $num -= $order;
- }
- if (($num > 0)and($num < 11)){
- array_push($words, smallNumberToText($num, $isFemale));
- return implode($words," ");
- }
- if (($num > 10)and($num < 20)){
- array_push($words, smallNumberToText($num, $isFemale));
- return implode($words," ");
- }
- if ($num > 19){
- $order = floor($num/10)*10;
- array_push($words, smallNumberToText($order, $isFemale));
- $num -= $order;
- if($num==0){return implode($words," ");}
- array_push($words, smallNumberToText($num, $isFemale));
- return implode($words," ");
- }
- }
- /*
- Преобразует числа от 0 до 999 в текст. Параметр $isFemale равен нулю,
- если мы считаем число для мужского рода (один рубль),
- и 1 — для женского (одна тысяча)
- */
- function smallNumberToText($number, $isFemale) {
- $spelling = array(
- 0 => 'ноль', 10 => 'десять', 100 => 'сто',
- 1 => 'один', 11 => 'одиннадцать', 20 => 'двадцать', 200 => 'двести',
- 2 => 'два', 12 => 'двенадцать', 30 => 'тридцать', 300 => 'триста',
- 3 => 'три', 13 => 'тринадцать', 40 => 'сорок', 400 => 'четыреста',
- 4 => 'четыре', 14 => 'четырнадцать', 50 => 'пятьдесят', 500 => 'пятьсот',
- 5 => 'пять', 15 => 'пятнадцать', 60 => 'шестьдесят', 600 => 'шестьсот',
- 6 => 'шесть', 16 => 'шестнадцать', 70 => 'семьдесят', 700 => 'семьсот',
- 7 => 'семь', 17 => 'семнадцать', 80 => 'восемьдесят', 800 => 'восемьсот',
- 8 => 'восемь', 18 => 'восемнадцать', 90 => 'девяносто', 900 => 'девятьсот',
- 9 => 'девять', 19 => 'девятнадцать'
- );
- $femaleSpelling = array(
- 1 => 'одна', 2 => 'две'
- );
- /* DIY */
- if ($isFemale == 1){
- $spelling = array_replace($spelling, $femaleSpelling);
- }
- $text = $spelling[$number];
- return $text;
- }
- function numberToText($number) {
- $result = array();
- $numbers = array_reverse(explode(" ", number_format($number, 0, ',', ' ')));
- foreach ($numbers as $key => $value) {
- if (($key == 1) and ((int)$value == 0)){
- continue;
- }
- if ((count($numbers)>1)and($value==0)){
- $wordForm = getWordForm((int)$value, $key);
- array_push($result, $wordForm);
- continue;
- }
- $smallNum = spellSmallNumber((int)$value, $key);
- $wordForm = getWordForm((int)$value, $key);
- $array = array($smallNum, $wordForm);
- array_push($result, implode(" ", $array));
- }
- $result = array_reverse($result);
- return implode(" ", $result);
- }
- // Вызовем функцию несколько раз
- $amount1 = mt_rand(1,99999999);
- $text1 = numberToText($amount1);
- echo "На вашем счету {$text1}<br>";
- $amount2 = mt_rand(1,99999999);
- $text2 = numberToText($amount2);
- echo "На вашем счету {$text2}<br>";
- $amount3 = mt_rand(1,99999999);
- $text3 = numberToText($amount3);
- echo "На вашем счету {$text3}<br>";
- $amount4 = mt_rand(1,99999999);
- $text4 = numberToText($amount4);
- echo "На вашем счету {$text4}<br>";
- /*
- $number = 7000000;
- echo numberToText($number); */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement