[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Support/Numerizer/Locale/ -> De.php (source)

   1  <?php
   2  /**
   3   * Copyright 2010-2014 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                  create_function(
  90                      '$m',
  91                      'return ' . $tp_replacement . ' + (isset($m[1]) ? (int)$m[1] : 0);'
  92                  ),
  93                  $string);
  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                  create_function(
 107                      '$m',
 108                      '$factor = (int)$m[1]; if (!$factor) $factor = 1; return (' . $bp_replacement . ' * $factor)' . ($bp_replacement == 100 ? ' . ($m[2] ? "und" : "")' : ' . "und"') . ' . $m[2];'
 109                  ),
 110                  $string);
 111              $string = $this->_andition($string);
 112          }
 113          return $string;
 114      }
 115  
 116      protected function _andition($string)
 117      {
 118          while (preg_match('/(\d+)((?: *und *)+)(\d*)(?=\w|$)/i', $string, $sc, PREG_OFFSET_CAPTURE)) {
 119              $string = substr($string, 0, $sc[1][1]) . ((int)$sc[1][0] + (int)$sc[3][0]) . substr($string, $sc[3][1] + strlen($sc[3][0]));
 120          }
 121          return $string;
 122      }
 123  
 124  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1