Detody

UPDATED NOTOWORDS PHP

Oct 25th, 2022 (edited)
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2. function convert_number_to_words($number) {
  3.    
  4.     $hyphen      = '-';
  5.     $conjunction = '  ';
  6.     $separator   = ' ';
  7.     $decimal     = ' point ';
  8.     $dictionary  = array(
  9.         0                   => 'Zero',
  10.         1                   => 'One',
  11.         2                   => 'Two',
  12.         3                   => 'Three',
  13.         4                   => 'Four',
  14.         5                   => 'Five',
  15.         6                   => 'Six',
  16.         7                   => 'Seven',
  17.         8                   => 'Eight',
  18.         9                   => 'Nine',
  19.         10                  => 'Ten',
  20.         11                  => 'Eleven',
  21.         12                  => 'Twelve',
  22.         13                  => 'Thirteen',
  23.         14                  => 'Fourteen',
  24.         15                  => 'Fifteen',
  25.         16                  => 'Sixteen',
  26.         17                  => 'Seventeen',
  27.         18                  => 'Eighteen',
  28.         19                  => 'Nineteen',
  29.         20                  => 'Twenty',
  30.         30                  => 'Thirty',
  31.         40                  => 'Fourty',
  32.         50                  => 'Fifty',
  33.         60                  => 'Sixty',
  34.         70                  => 'Seventy',
  35.         80                  => 'Eighty',
  36.         90                  => 'Ninety',
  37.         100                 => 'Hundred',
  38.         1000                => 'Thousand',
  39.         1000000             => 'Million',
  40.     );
  41.    
  42.     if (!is_numeric($number)) {
  43.         return false;
  44.     }
  45.    
  46.     if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
  47.         echo "Max. of 7   digits only!";
  48.         return false;
  49.     }
  50.  
  51.     if ($number >= 10000000) {
  52.         echo "Max. of 7 digits only!"; goto end;
  53.     }
  54.  
  55.  
  56.     if ($number < 0) {
  57.         echo "Negative Values not Accepted"; goto end;
  58.     }
  59.  
  60.     $string = $fraction = null;
  61.    
  62.     if (strpos($number, '.') !== false) {
  63.         list($number, $fraction) = explode('.', $number);
  64.     }
  65.    
  66.     switch (true) {
  67.         case $number < 21:
  68.             $string = $dictionary[$number];
  69.             break;
  70.         case $number < 100:
  71.             $tens   = ((int) ($number / 10)) * 10;
  72.             $units  = $number % 10;
  73.             $string = $dictionary[$tens];
  74.             if ($units) {
  75.                 $string .= $hyphen . $dictionary[$units];
  76.             }
  77.             break;
  78.         case $number < 1000:
  79.             $hundreds  = $number / 100;
  80.             $remainder = $number % 100;
  81.             $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
  82.             if ($remainder) {
  83.                 $string .= $conjunction . convert_number_to_words($remainder);
  84.             }
  85.             break;
  86.         default:
  87.             $baseUnit = pow(1000, floor(log($number, 1000)));
  88.             $numBaseUnits = (int) ($number / $baseUnit);
  89.             $remainder = $number % $baseUnit;
  90.             $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
  91.             if ($remainder) {
  92.                 $string .= $remainder < 100 ? $conjunction : $separator;
  93.                 $string .= convert_number_to_words($remainder);
  94.             }
  95.             break;
  96.     }
  97.    
  98.     if (null !== $fraction && is_numeric($fraction)) {
  99.         $string .= $decimal;
  100.         $words = array();
  101.         foreach (str_split((string) $fraction) as $number) {
  102.             $words[] = $dictionary[$number];
  103.         }
  104.         $string .= implode(' ', $words);
  105.     }
  106.    
  107.     return $string;
  108.  
  109.     end:
  110. }
  111.  
  112. echo '<b>'.convert_number_to_words($_POST['num']).'</b>';
  113.  
  114. ?>
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment