I18N_Arabic
[ class tree: I18N_Arabic ] [ index: I18N_Arabic ] [ all elements ]

Source for file Numbers.php

Documentation is available at Numbers.php

  1. <?php
  2. /**
  3.  * ----------------------------------------------------------------------
  4.  *  
  5.  * Copyright (c) 2006-2012 Khaled Al-Sham'aa.
  6.  *  
  7.  * http://www.ar-php.org
  8.  *  
  9.  * PHP Version 5
  10.  *  
  11.  * ----------------------------------------------------------------------
  12.  *  
  13.  * LICENSE
  14.  *
  15.  * This program is open source product; you can redistribute it and/or
  16.  * modify it under the terms of the GNU Lesser General Public License (LGPL)
  17.  * as published by the Free Software Foundation; either version 3
  18.  * of the License, or (at your option) any later version.
  19.  *  
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU Lesser General Public License for more details.
  24.  *  
  25.  * You should have received a copy of the GNU Lesser General Public License
  26.  * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl.txt>.
  27.  *  
  28.  * ----------------------------------------------------------------------
  29.  *  
  30.  * Class Name: Spell numbers in the Arabic idiom
  31.  *  
  32.  * Filename:   Numbers.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    Spell numbers in the Arabic idiom
  37.  *  
  38.  * ----------------------------------------------------------------------
  39.  *  
  40.  * Spell numbers in the Arabic idiom
  41.  *
  42.  * PHP class to spell numbers in the Arabic idiom. This function is very
  43.  * useful for financial applications in Arabic for example.
  44.  *    
  45.  * If you ever have to create an Arabic PHP application built around invoicing or
  46.  * accounting, you might find this class useful. Its sole reason for existence is
  47.  * to help you translate integers into their spoken-word equivalents in Arabic
  48.  * language.
  49.  * 
  50.  * How is this useful? Well, consider the typical invoice: In addition to a
  51.  * description of the work done, the date, and the hourly or project cost, it always
  52.  * includes a total cost at the end, the amount that the customer is expected
  53.  * to pay.
  54.  *   
  55.  * To avoid any misinterpretation of the total amount, many organizations (mine
  56.  * included) put the amount in both words and figures; for example, $1,200 becomes
  57.  * "one thousand and two hundred dollars." You probably do the same thing every time
  58.  * you write a check.
  59.  * 
  60.  * Now take this scenario to a Web-based invoicing system. The actual data used to
  61.  * generate the invoice will be stored in a database as integers, both to save space
  62.  * and to simplify calculations. So when a printable invoice is generated, your Web
  63.  * application will need to convert those integers into words, this is more clarity
  64.  * and more personality.
  65.  * 
  66.  * This class will accept almost any numeric value and convert it into an equivalent
  67.  * string of words in written Arabic language (using Windows-1256 character set).
  68.  * The value can be any positive number up to 999,999,999 (users should not use
  69.  * commas). It will take care of feminine and Arabic grammar rules.
  70.  *
  71.  * Example:
  72.  * <code>
  73.  *     include('./I18N/Arabic.php');
  74.  *     $obj = new I18N_Arabic('Numbers');
  75.  *     
  76.  *     $obj->setFeminine(1);
  77.  *     $obj->setFormat(1);
  78.  *     
  79.  *     $integer = 2147483647;
  80.  *     
  81.  *     $text = $obj->int2str($integer);
  82.  *     
  83.  *     echo "<p align=\"right\"><b class=hilight>$integer</b><br />$text</p>";
  84.  * 
  85.  *     $obj->setFeminine(2);
  86.  *     $obj->setFormat(2);
  87.  *     
  88.  *     $integer = 2147483647;
  89.  *     
  90.  *     $text = $obj->int2str($integer);
  91.  *     
  92.  *     echo "<p align=\"right\"><b class=hilight>$integer</b><br />$text</p>";
  93.  * </code>
  94.  *             
  95.  * @category  I18N
  96.  * @package   I18N_Arabic
  97.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  98.  * @copyright 2006-2012 Khaled Al-Sham'aa
  99.  *    
  100.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  101.  * @link      http://www.ar-php.org
  102.  */
  103.  
  104. // New in PHP V5.3: Namespaces
  105. // namespace I18N\Arabic;
  106. // 
  107. // $obj = new I18N\Arabic\Numbers();
  108. // 
  109. // use I18N\Arabic;
  110. // $obj = new Arabic\Numbers();
  111. //
  112. // use I18N\Arabic\Numbers as Numbers;
  113. // $obj = new Numbers();
  114.  
  115. /**
  116.  * This PHP class spell numbers in the Arabic idiom
  117.  *  
  118.  * @category  I18N
  119.  * @package   I18N_Arabic
  120.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  121.  * @copyright 2006-2012 Khaled Al-Sham'aa
  122.  *    
  123.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  124.  * @link      http://www.ar-php.org
  125.  */ 
  126. {
  127.     private $_individual    array();
  128.     private $_complications array();
  129.     private $_arabicIndic   array();
  130.     private $_feminine      1;
  131.     private $_format        1;
  132.  
  133.     /**
  134.      * Loads initialize values
  135.      *
  136.      * @ignore
  137.      */         
  138.     public function __construct()
  139.     {
  140.         $xml simplexml_load_file(dirname(__FILE__).'/data/ArNumbers.xml');
  141.  
  142.         foreach ($xml->xpath("//individual/number[@gender='male']"as $num{
  143.             if (isset($num['grammar'])) {
  144.                 $grammar $num['grammar'];
  145.                 
  146.                 $this->_individual["{$num['value']}"][1]["$grammar"= (string)$num;
  147.             else {
  148.                 $this->_individual["{$num['value']}"][1= (string)$num;
  149.             }
  150.         
  151.         
  152.         foreach ($xml->xpath("//individual/number[@gender='female']"as $num{
  153.             if (isset($num['grammar'])) {
  154.                 $grammar $num['grammar'];
  155.                 
  156.                 $this->_individual["{$num['value']}"][2]["$grammar"= (string)$num;
  157.             else {
  158.                 $this->_individual["{$num['value']}"][2= (string)$num;
  159.             }
  160.         
  161.         
  162.         foreach ($xml->xpath("//individual/number[@value>19]"as $num{
  163.             if (isset($num['grammar'])) {
  164.                 $grammar $num['grammar'];
  165.                 
  166.                 $this->_individual["{$num['value']}"]["$grammar"= (string)$num;
  167.             else {
  168.                 $this->_individual["{$num['value']}"= (string)$num;
  169.             }
  170.         
  171.         
  172.         foreach ($xml->complications->number as $num{
  173.             $scale  $num['scale'];
  174.             $format $num['format'];
  175.             
  176.             $this->_complications["$scale"]["$format"= (string)$num;
  177.         
  178.         
  179.         foreach ($xml->arabicIndic->number as $html{
  180.             $value  $html['value'];
  181.             
  182.             $this->_arabicIndic["$value"$html;
  183.         
  184.     }
  185.     
  186.     /**
  187.      * Set feminine flag of the counted object
  188.      *      
  189.      * @param integer $value Counted object feminine
  190.      *                       (1 for masculine & 2 for feminine)
  191.      *      
  192.      * @return object $this to build a fluent interface
  193.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  194.      */
  195.     public function setFeminine($value)
  196.     {
  197.         if ($value == || $value == 2{
  198.             $this->_feminine $value;
  199.         }
  200.         
  201.         return $this;
  202.     }
  203.     
  204.     /**
  205.      * Set the grammar position flag of the counted object
  206.      *      
  207.      * @param integer $value Grammar position of counted object
  208.      *                        (1 if Marfoua & 2 if Mansoub or Majrour)
  209.      *                            
  210.      * @return object $this to build a fluent interface
  211.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  212.      */
  213.     public function setFormat($value)
  214.     {
  215.         if ($value == || $value == 2{
  216.             $this->_format $value;
  217.         }
  218.         
  219.         return $this;
  220.     }
  221.     
  222.     /**
  223.      * Get the feminine flag of counted object
  224.      *      
  225.      * @return integer return current setting of counted object feminine flag
  226.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  227.      */
  228.     public function getFeminine()
  229.     {
  230.         return $this->_feminine;
  231.     }
  232.     
  233.     /**
  234.      * Get the grammer position flag of counted object
  235.      *      
  236.      * @return integer return current setting of counted object grammer
  237.      *                  position flag
  238.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  239.      */
  240.     public function getFormat()
  241.     {
  242.         return $this->_format;
  243.     }
  244.     
  245.     /**
  246.      * Spell integer number in Arabic idiom
  247.      *      
  248.      * @param integer $number The number you want to spell in Arabic idiom
  249.      *                    
  250.      * @return string The Arabic idiom that spells inserted number
  251.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  252.      */
  253.     public function int2str($number)
  254.     {
  255.         if ($number 0{
  256.             $string 'سالب ';
  257.             $number = (string) -$number;
  258.         else {
  259.             $string '';
  260.         }
  261.         
  262.         $temp explode('.'$number);
  263.  
  264.         $string .= $this->subInt2str($temp[0]);
  265.  
  266.         if (!empty($temp[1])) {
  267.             $dec     $this->subInt2str($temp[1]);
  268.             $string .= ' فاصلة ' $dec
  269.         }
  270.         
  271.         return $string;
  272.     }
  273.     
  274.     /**
  275.      * Spell integer number in Arabic idiom
  276.      *      
  277.      * @param integer $number The number you want to spell in Arabic idiom
  278.      * @param logical $zero   Present leading zero if true [default is true]
  279.      *      
  280.      * @return string The Arabic idiom that spells inserted number
  281.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  282.      */
  283.     protected function subInt2str($number$zero true)
  284.     {
  285.         $blocks array();
  286.         $items  array();
  287.         $zeros  '';
  288.         $string '';
  289.         $number ($zero != falsetrim($numbertrim((float)$number);
  290.         
  291.         if ($number 0{
  292.         
  293.             //--- by Jnom: handle left zero
  294.             // http://www.itkane.com
  295.             // jnom23@gmail.com
  296.             if ($zero != false{
  297.                 $fulnum $number;
  298.                 while (($fulnum[0]== '0'{
  299.                     $zeros 'صفر '.$zeros;
  300.                     $fulnum substr($fulnum1strlen($fulnum));
  301.                 };
  302.             };
  303.             //---/
  304.  
  305.             while (strlen($number3{
  306.                 array_push($blockssubstr($number-3));
  307.                 $number substr($number0strlen($number3);
  308.             }
  309.             array_push($blocks$number);
  310.             
  311.             $blocks_num count($blocks1;
  312.   
  313.             for ($i $blocks_num$i >= 0$i--{
  314.                 $number floor($blocks[$i]);
  315.   
  316.                 $text $this->writtenBlock($number);
  317.                 if ($text{
  318.                     if ($number == && $i != 0{
  319.                         $text $this->_complications[$i][4];
  320.                     elseif ($number == && $i != 0{
  321.                         $text $this->_complications[$i][$this->_format];
  322.                     elseif ($number && $number 11 && $i != 0{
  323.                         $text .= ' ' $this->_complications[$i][3];
  324.                     elseif ($i != 0{
  325.                         $text .= ' ' $this->_complications[$i][4];
  326.                     }
  327.                     
  328.                     //--- by Jnom: handle left zero
  329.                     if ($text != '' && $zeros != '' && $zero != false{
  330.                         $text  $zeros.' '.$text;
  331.                         $zeros '';
  332.                     };
  333.                     //---/
  334.  
  335.                     array_push($items$text);
  336.                 }
  337.             }
  338.             
  339.             $string implode(' و '$items);
  340.         else {
  341.             $string 'صفر';
  342.         }
  343.         return $string;
  344.     }
  345.     
  346.     /**
  347.      * Spell sub block number of three digits max in Arabic idiom
  348.      *      
  349.      * @param integer $number Sub block number of three digits max you want to
  350.      *                         spell in Arabic idiom
  351.      *                      
  352.      * @return string The Arabic idiom that spells inserted sub block
  353.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  354.      */
  355.     protected function writtenBlock($number)
  356.     {
  357.         $items  array();
  358.         $string '';
  359.         
  360.         if ($number 99{
  361.             $hundred floor($number 100100;
  362.             $number  $number 100;
  363.             
  364.             if ($hundred == 200{
  365.                 array_push($items$this->_individual[$hundred][$this->_format]);
  366.             else {
  367.                 array_push($items$this->_individual[$hundred]);
  368.             }
  369.         }
  370.         
  371.         if ($number != 0{
  372.             if ($number == || $number == 12{
  373.                 array_push($items$this->_individual[$number]
  374.                                                     [$this->_feminine]
  375.                                                     [$this->_format]);
  376.             elseif ($number 20{
  377.                 array_push($items$this->_individual[$number][$this->_feminine]);
  378.             else {
  379.                 $ones $number 10;
  380.                 $tens floor($number 1010;
  381.                 
  382.                 if ($ones == 2{
  383.                     array_push($items$this->_individual[$ones]
  384.                                                         [$this->_feminine]
  385.                                                         [$this->_format]);
  386.                 elseif ($ones 0{
  387.                     array_push($items$this->_individual[$ones][$this->_feminine]);
  388.                 }
  389.                 
  390.                 array_push($items$this->_individual[$tens][$this->_format]);
  391.             }
  392.         }
  393.         
  394.         $items array_diff($itemsarray(''));
  395.         
  396.         $string implode(' و '$items);
  397.         
  398.         return $string;
  399.     }
  400.  
  401.     
  402.     /**
  403.      * Represent integer number in Arabic-Indic digits using HTML entities
  404.      *      
  405.      * @param integer $number The number you want to present in Arabic-Indic digits
  406.      *                         using HTML entities
  407.      *                    
  408.      * @return string The Arabic-Indic digits represent inserted integer number using
  409.      *                 HTML entities
  410.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  411.      */
  412.     public function int2indic($number)
  413.     {
  414.         $str strtr("$number"$this->_arabicIndic);
  415.  
  416.         return $str;
  417.     }
  418. }

Documentation generated on Tue, 17 Jan 2012 09:18:22 +0200 by phpDocumentor 1.4.0