Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * @author   Chuck Hagenbuch <chuck@horde.org>
   6   * @author   Jan Schneider <jan@horde.org>
   7   * @license  http://www.horde.org/licenses/bsd BSD
   8   * @category Horde
   9   * @package  Support
  10   */
  11  
  12  /**
  13   * @author   Chuck Hagenbuch <chuck@horde.org>
  14   * @author   Jan Schneider <jan@horde.org>
  15   * @license  http://www.horde.org/licenses/bsd BSD
  16   * @category Horde
  17   * @package  Support
  18   */
  19  class Horde_Support_Numerizer_Locale_De extends Horde_Support_Numerizer_Locale_Base
  20  {
  21      public $DIRECT_NUMS = array(
  22          'dreizehn' => 13,
  23          'vierzehn' => 14,
  24          'fünfzehn' => 15,
  25          'sechzehn' => 16,
  26          'siebzehn' => 17,
  27          'achtzehn' => 18,
  28          'neunzehn' => 19,
  29          'ein[se]?' => 1,
  30          'zwei' => 2,
  31          'zwo' => 2,
  32          'drei' => 3,
  33          'vier' => 4,
  34          'fünf' => 5,
  35          'sechs' => 6,
  36          'sieben' => 7,
  37          'acht' => 8,
  38          'neun' => 9,
  39          'zehn' => 10,
  40          'elf' => 11,
  41          'zwölf' => 12,
  42      );
  43  
  44      public $TEN_PREFIXES = array(
  45          'zwanzig' => 20,
  46          'dreißig' => 30,
  47          'vierzig' => 40,
  48          'fünfzig' => 50,
  49          'sechzig' => 60,
  50          'siebzig' => 70,
  51          'achtzig' => 80,
  52          'neunzig' => 90,
  53      );
  54  
  55      public $BIG_PREFIXES = array(
  56          'hundert' => 100,
  57          'tausend' => 1000,
  58          'million *' => 1000000,
  59          'milliarde *' => 1000000000,
  60          'billion *' => 1000000000000,
  61      );
  62  
  63      /**
  64       * Rules:
  65       *
  66       * - there are irregular word for 11 and 12 like in English
  67       * - numbers below one million are written together (1 M = "eine Million", 100 = "einhundert")
  68       * - "a" is declinable (see above, "one" = "eins", "a" = "ein/eine")
  69       * - numbers below 100 are flipped compared to english, and have an "and = "und" (21 = "twenty-one" = "einundzwanzig")
  70       */
  71      public function numerize($string)
  72      {
  73          $string = $this->_replaceTenPrefixes($string);
  74          $string = $this->_directReplacements($string);
  75          $string = $this->_replaceBigPrefixes($string);
  76          $string = $this->_fractionalAddition($string);
  77  
  78          return $string;
  79      }
  80  
  81      /**
  82       * ten, twenty, etc.
  83       */
  84      protected function _replaceTenPrefixes($string)
  85      {
  86          foreach ($this->TEN_PREFIXES as $tp => $tp_replacement) {
  87              $string = preg_replace_callback(
  88                  "/(?:$tp)( *\d(?=[^\d]|\$))*/i",
  89                  function ($m) use ($tp_replacement) {
  90                      return $tp_replacement + (isset($m[1]) ? (int)$m[1] : 0);
  91                  },
  92                  $string
  93              );
  94          }
  95          return $string;
  96      }
  97  
  98      /**
  99       * hundreds, thousands, millions, etc.
 100       */
 101      protected function _replaceBigPrefixes($string)
 102      {
 103          foreach ($this->BIG_PREFIXES as $bp => $bp_replacement) {
 104              $string = preg_replace_callback(
 105                  '/(\d*) *' . $bp . '(\d?)/i',
 106                  function ($m) use ($bp_replacement) {
 107                      $factor = (int)$m[1];
 108                      if (!$factor) {
 109                          $factor = 1;
 110                      }
 111                      return ($bp_replacement * $factor)
 112                          . ($bp_replacement == 100 ? ($m[2] ? 'und' : '') : 'und')
 113                          . $m[2];
 114                  },
 115                  $string
 116              );
 117              $string = $this->_andition($string);
 118          }
 119          return $string;
 120      }
 121  
 122      protected function _andition($string)
 123      {
 124          while (preg_match('/(\d+)((?: *und *)+)(\d*)(?=\w|$)/i', $string, $sc, PREG_OFFSET_CAPTURE)) {
 125              $string = substr($string, 0, $sc[1][1])
 126                  . ((int)$sc[1][0] + (int)$sc[3][0])
 127                  . substr($string, $sc[3][1] + strlen($sc[3][0]));
 128          }
 129          return $string;
 130      }
 131  
 132  }