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

Source for file Functions.php

Documentation is available at Functions.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  *
  21.  * @category    PHPExcel
  22.  * @package        PHPExcel_Calculation
  23.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  35.     require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /** MAX_VALUE */
  40. define('MAX_VALUE', 1.2e308);
  41.  
  42. /** 2 / PI */
  43. define('M_2DIVPI', 0.63661977236758134307553505349006);
  44.  
  45. /** MAX_ITERATIONS */
  46. define('MAX_ITERATIONS', 256);
  47.  
  48. /** PRECISION */
  49. define('PRECISION', 8.88E-016);
  50.  
  51.  
  52. /**
  53.  * PHPExcel_Calculation_Functions
  54.  *
  55.  * @category    PHPExcel
  56.  * @package        PHPExcel_Calculation
  57.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  58.  */
  59.  
  60.     /** constants */
  61.     const COMPATIBILITY_EXCEL        = 'Excel';
  62.     const COMPATIBILITY_GNUMERIC    = 'Gnumeric';
  63.     const COMPATIBILITY_OPENOFFICE    = 'OpenOfficeCalc';
  64.  
  65.     const RETURNDATE_PHP_NUMERIC    = 'P';
  66.     const RETURNDATE_PHP_OBJECT        = 'O';
  67.     const RETURNDATE_EXCEL            = 'E';
  68.  
  69.  
  70.     /**
  71.      *    Compatibility mode to use for error checking and responses
  72.      *
  73.      *    @access    private
  74.      *    @var string 
  75.      */
  76.     protected static $compatibilityMode    = self::COMPATIBILITY_EXCEL;
  77.  
  78.     /**
  79.      *    Data Type to use when returning date values
  80.      *
  81.      *    @access    private
  82.      *    @var string 
  83.      */
  84.     protected static $ReturnDateType    = self::RETURNDATE_EXCEL;
  85.  
  86.     /**
  87.      *    List of error codes
  88.      *
  89.      *    @access    private
  90.      *    @var array 
  91.      */
  92.     protected static $_errorCodes    = array( 'null'                => '#NULL!',
  93.                                              'divisionbyzero'    => '#DIV/0!',
  94.                                              'value'            => '#VALUE!',
  95.                                              'reference'        => '#REF!',
  96.                                              'name'                => '#NAME?',
  97.                                              'num'                => '#NUM!',
  98.                                              'na'                => '#N/A',
  99.                                              'gettingdata'        => '#GETTING_DATA'
  100.                                            );
  101.  
  102.  
  103.     /**
  104.      *    Set the Compatibility Mode
  105.      *
  106.      *    @access    public
  107.      *    @category Function Configuration
  108.      *    @param     string        $compatibilityMode        Compatibility Mode
  109.      *                                                 Permitted values are:
  110.      *                                                     PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL            'Excel'
  111.      *                                                     PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC        'Gnumeric'
  112.      *                                                     PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE    'OpenOfficeCalc'
  113.      *    @return     boolean    (Success or Failure)
  114.      */
  115.     public static function setCompatibilityMode($compatibilityMode) {
  116.         if (($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
  117.             ($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
  118.             ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  119.             self::$compatibilityMode = $compatibilityMode;
  120.             return True;
  121.         }
  122.         return False;
  123.     }    //    function setCompatibilityMode()
  124.  
  125.  
  126.     /**
  127.      *    Return the current Compatibility Mode
  128.      *
  129.      *    @access    public
  130.      *    @category Function Configuration
  131.      *    @return     string        Compatibility Mode
  132.      *                             Possible Return values are:
  133.      *                                 PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL            'Excel'
  134.      *                                 PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC        'Gnumeric'
  135.      *                                 PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE    'OpenOfficeCalc'
  136.      */
  137.     public static function getCompatibilityMode() {
  138.         return self::$compatibilityMode;
  139.     }    //    function getCompatibilityMode()
  140.  
  141.  
  142.     /**
  143.      *    Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
  144.      *
  145.      *    @access    public
  146.      *    @category Function Configuration
  147.      *    @param     string    $returnDateType            Return Date Format
  148.      *                                                 Permitted values are:
  149.      *                                                     PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC        'P'
  150.      *                                                     PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT        'O'
  151.      *                                                     PHPExcel_Calculation_Functions::RETURNDATE_EXCEL            'E'
  152.      *    @return     boolean                            Success or failure
  153.      */
  154.     public static function setReturnDateType($returnDateType) {
  155.         if (($returnDateType == self::RETURNDATE_PHP_NUMERIC) ||
  156.             ($returnDateType == self::RETURNDATE_PHP_OBJECT) ||
  157.             ($returnDateType == self::RETURNDATE_EXCEL)) {
  158.             self::$ReturnDateType = $returnDateType;
  159.             return True;
  160.         }
  161.         return False;
  162.     }    //    function setReturnDateType()
  163.  
  164.  
  165.     /**
  166.      *    Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
  167.      *
  168.      *    @access    public
  169.      *    @category Function Configuration
  170.      *    @return     string        Return Date Format
  171.      *                             Possible Return values are:
  172.      *                                 PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC        'P'
  173.      *                                 PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT        'O'
  174.      *                                 PHPExcel_Calculation_Functions::RETURNDATE_EXCEL            'E'
  175.      */
  176.     public static function getReturnDateType() {
  177.         return self::$ReturnDateType;
  178.     }    //    function getReturnDateType()
  179.  
  180.  
  181.     /**
  182.      *    DUMMY
  183.      *
  184.      *    @access    public
  185.      *    @category Error Returns
  186.      *    @return    string    #Not Yet Implemented
  187.      */
  188.     public static function DUMMY() {
  189.         return '#Not Yet Implemented';
  190.     }    //    function DUMMY()
  191.  
  192.  
  193.     /**
  194.      *    DIV0
  195.      *
  196.      *    @access    public
  197.      *    @category Error Returns
  198.      *    @return    string    #Not Yet Implemented
  199.      */
  200.     public static function DIV0() {
  201.         return self::$_errorCodes['divisionbyzero'];
  202.     }    //    function DIV0()
  203.  
  204.  
  205.     /**
  206.      *    NA
  207.      *
  208.      *    Excel Function:
  209.      *        =NA()
  210.      *
  211.      *    Returns the error value #N/A
  212.      *        #N/A is the error value that means "no value is available."
  213.      *
  214.      *    @access    public
  215.      *    @category Logical Functions
  216.      *    @return    string    #N/A!
  217.      */
  218.     public static function NA() {
  219.         return self::$_errorCodes['na'];
  220.     }    //    function NA()
  221.  
  222.  
  223.     /**
  224.      *    NaN
  225.      *
  226.      *    Returns the error value #NUM!
  227.      *
  228.      *    @access    public
  229.      *    @category Error Returns
  230.      *    @return    string    #NUM!
  231.      */
  232.     public static function NaN() {
  233.         return self::$_errorCodes['num'];
  234.     }    //    function NaN()
  235.  
  236.  
  237.     /**
  238.      *    NAME
  239.      *
  240.      *    Returns the error value #NAME?
  241.      *
  242.      *    @access    public
  243.      *    @category Error Returns
  244.      *    @return    string    #NAME?
  245.      */
  246.     public static function NAME() {
  247.         return self::$_errorCodes['name'];
  248.     }    //    function NAME()
  249.  
  250.  
  251.     /**
  252.      *    REF
  253.      *
  254.      *    Returns the error value #REF!
  255.      *
  256.      *    @access    public
  257.      *    @category Error Returns
  258.      *    @return    string    #REF!
  259.      */
  260.     public static function REF() {
  261.         return self::$_errorCodes['reference'];
  262.     }    //    function REF()
  263.  
  264.  
  265.     /**
  266.      *    NULL
  267.      *
  268.      *    Returns the error value #NULL!
  269.      *
  270.      *    @access    public
  271.      *    @category Error Returns
  272.      *    @return    string    #REF!
  273.      */
  274.     public static function NULL() {
  275.         return self::$_errorCodes['null'];
  276.     }    //    function NULL()
  277.  
  278.  
  279.     /**
  280.      *    VALUE
  281.      *
  282.      *    Returns the error value #VALUE!
  283.      *
  284.      *    @access    public
  285.      *    @category Error Returns
  286.      *    @return    string    #VALUE!
  287.      */
  288.     public static function VALUE() {
  289.         return self::$_errorCodes['value'];
  290.     }    //    function VALUE()
  291.  
  292.  
  293.     public static function isMatrixValue($idx) {
  294.         return ((substr_count($idx,'.') <= 1) || (preg_match('/\.[A-Z]/',$idx) > 0));
  295.     }
  296.  
  297.  
  298.     public static function isValue($idx) {
  299.         return (substr_count($idx,'.') == 0);
  300.     }
  301.  
  302.  
  303.     public static function isCellValue($idx) {
  304.         return (substr_count($idx,'.') > 1);
  305.     }
  306.  
  307.  
  308.     public static function _ifCondition($condition) {
  309.         $condition    = PHPExcel_Calculation_Functions::flattenSingleValue($condition);
  310.         if (!in_array($condition{0},array('>', '<', '='))) {
  311.             if (!is_numeric($condition)) { $condition = PHPExcel_Calculation::_wrapResult(strtoupper($condition)); }
  312.             return '='.$condition;
  313.         } else {
  314.             preg_match('/([<>=]+)(.*)/',$condition,$matches);
  315.             list(,$operator,$operand) = $matches;
  316.             if (!is_numeric($operand)) { $operand = PHPExcel_Calculation::_wrapResult(strtoupper($operand)); }
  317.             return $operator.$operand;
  318.         }
  319.     }    //    function _ifCondition()
  320.  
  321.  
  322.     /**
  323.      *    ERROR_TYPE
  324.      *
  325.      *    @param    mixed    $value    Value to check
  326.      *    @return    boolean 
  327.      */
  328.     public static function ERROR_TYPE($value = '') {
  329.         $value    = self::flattenSingleValue($value);
  330.  
  331.         $i = 1;
  332.         foreach(self::$_errorCodes as $errorCode) {
  333.             if ($value == $errorCode) {
  334.                 return $i;
  335.             }
  336.             ++$i;
  337.         }
  338.         return self::$_errorCodes['na'];
  339.     }    //    function ERROR_TYPE()
  340.  
  341.  
  342.     /**
  343.      *    IS_BLANK
  344.      *
  345.      *    @param    mixed    $value    Value to check
  346.      *    @return    boolean 
  347.      */
  348.     public static function IS_BLANK($value=null) {
  349.         if (!is_null($value)) {
  350.             $value    = self::flattenSingleValue($value);
  351.         }
  352.  
  353.         return is_null($value);
  354.     }    //    function IS_BLANK()
  355.  
  356.  
  357.     /**
  358.      *    IS_ERR
  359.      *
  360.      *    @param    mixed    $value    Value to check
  361.      *    @return    boolean 
  362.      */
  363.     public static function IS_ERR($value = '') {
  364.         $value        = self::flattenSingleValue($value);
  365.  
  366.         return self::IS_ERROR($value) && (!self::IS_NA($value));
  367.     }    //    function IS_ERR()
  368.  
  369.  
  370.     /**
  371.      *    IS_ERROR
  372.      *
  373.      *    @param    mixed    $value    Value to check
  374.      *    @return    boolean 
  375.      */
  376.     public static function IS_ERROR($value = '') {
  377.         $value        = self::flattenSingleValue($value);
  378.  
  379.         return in_array($value, array_values(self::$_errorCodes));
  380.     }    //    function IS_ERROR()
  381.  
  382.  
  383.     /**
  384.      *    IS_NA
  385.      *
  386.      *    @param    mixed    $value    Value to check
  387.      *    @return    boolean 
  388.      */
  389.     public static function IS_NA($value = '') {
  390.         $value        = self::flattenSingleValue($value);
  391.  
  392.         return ($value === self::$_errorCodes['na']);
  393.     }    //    function IS_NA()
  394.  
  395.  
  396.     /**
  397.      *    IS_EVEN
  398.      *
  399.      *    @param    mixed    $value    Value to check
  400.      *    @return    boolean 
  401.      */
  402.     public static function IS_EVEN($value = 0) {
  403.         $value        = self::flattenSingleValue($value);
  404.  
  405.         if ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
  406.             return self::$_errorCodes['value'];
  407.         }
  408.         return ($value % 2 == 0);
  409.     }    //    function IS_EVEN()
  410.  
  411.  
  412.     /**
  413.      *    IS_ODD
  414.      *
  415.      *    @param    mixed    $value    Value to check
  416.      *    @return    boolean 
  417.      */
  418.     public static function IS_ODD($value = null) {
  419.         $value    = self::flattenSingleValue($value);
  420.  
  421.         if ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
  422.             return self::$_errorCodes['value'];
  423.         }
  424.         return (abs($value) % 2 == 1);
  425.     }    //    function IS_ODD()
  426.  
  427.  
  428.     /**
  429.      *    IS_NUMBER
  430.      *
  431.      *    @param    mixed    $value        Value to check
  432.      *    @return    boolean 
  433.      */
  434.     public static function IS_NUMBER($value = 0) {
  435.         $value        = self::flattenSingleValue($value);
  436.  
  437.         if (is_string($value)) {
  438.             return False;
  439.         }
  440.         return is_numeric($value);
  441.     }    //    function IS_NUMBER()
  442.  
  443.  
  444.     /**
  445.      *    IS_LOGICAL
  446.      *
  447.      *    @param    mixed    $value        Value to check
  448.      *    @return    boolean 
  449.      */
  450.     public static function IS_LOGICAL($value = true) {
  451.         $value        = self::flattenSingleValue($value);
  452.  
  453.         return is_bool($value);
  454.     }    //    function IS_LOGICAL()
  455.  
  456.  
  457.     /**
  458.      *    IS_TEXT
  459.      *
  460.      *    @param    mixed    $value        Value to check
  461.      *    @return    boolean 
  462.      */
  463.     public static function IS_TEXT($value = '') {
  464.         $value        = self::flattenSingleValue($value);
  465.  
  466.         return is_string($value);
  467.     }    //    function IS_TEXT()
  468.  
  469.  
  470.     /**
  471.      *    IS_NONTEXT
  472.      *
  473.      *    @param    mixed    $value        Value to check
  474.      *    @return    boolean 
  475.      */
  476.     public static function IS_NONTEXT($value = '') {
  477.         return !self::IS_TEXT($value);
  478.     }    //    function IS_NONTEXT()
  479.  
  480.  
  481.     /**
  482.      *    VERSION
  483.      *
  484.      *    @return    string    Version information
  485.      */
  486.     public static function VERSION() {
  487.         return 'PHPExcel 1.7.6, 2011-02-27';
  488.     }    //    function VERSION()
  489.  
  490.  
  491.     /**
  492.      *    N
  493.      *
  494.      *    Returns a value converted to a number
  495.      *
  496.      *    @param    value        The value you want converted
  497.      *    @return    number        N converts values listed in the following table
  498.      *         If value is or refers to N returns
  499.      *         A number            That number
  500.      *         A date                The serial number of that date
  501.      *         TRUE                1
  502.      *         FALSE                0
  503.      *         An error value        The error value
  504.      *         Anything else        0
  505.      */
  506.     public static function N($value) {
  507.         while (is_array($value)) {
  508.             $value = array_shift($value);
  509.         }
  510.  
  511.         switch (gettype($value)) {
  512.             case 'double'    :
  513.             case 'float'    :
  514.             case 'integer'    :
  515.                 return $value;
  516.                 break;
  517.             case 'boolean'    :
  518.                 return (integer) $value;
  519.                 break;
  520.             case 'string'    :
  521.                 //    Errors
  522.                 if ((strlen($value) > 0) && ($value{0} == '#')) {
  523.                     return $value;
  524.                 }
  525.                 break;
  526.         }
  527.         return 0;
  528.     }    //    function N()
  529.  
  530.  
  531.     /**
  532.      *    TYPE
  533.      *
  534.      *    Returns a number that identifies the type of a value
  535.      *
  536.      *    @param    value        The value you want tested
  537.      *    @return    number        N converts values listed in the following table
  538.      *         If value is or refers to N returns
  539.      *         A number            1
  540.      *         Text                2
  541.      *         Logical Value        4
  542.      *         An error value        16
  543.      *         Array or Matrix        64
  544.      */
  545.     public static function TYPE($value) {
  546.         $value    = self::flattenArrayIndexed($value);
  547.         if (is_array($value) && (count($value) > 1)) {
  548.             $a = array_keys($value);
  549.             $a = array_pop($a);
  550.             //    Range of cells is an error
  551.             if (self::isCellValue($a)) {
  552.                 return 16;
  553.             //    Test for Matrix
  554.             } elseif (self::isMatrixValue($a)) {
  555.                 return 64;
  556.             }
  557.         } elseif(count($value) == 0) {
  558.             //    Empty Cell
  559.             return 1;
  560.         }
  561.         $value    = self::flattenSingleValue($value);
  562.  
  563.         if ((is_float($value)) || (is_int($value))) {
  564.                 return 1;
  565.         } elseif(is_bool($value)) {
  566.                 return 4;
  567.         } elseif(is_array($value)) {
  568.                 return 64;
  569.                 break;
  570.         } elseif(is_string($value)) {
  571.             //    Errors
  572.             if ((strlen($value) > 0) && ($value{0} == '#')) {
  573.                 return 16;
  574.             }
  575.             return 2;
  576.         }
  577.         return 0;
  578.     }    //    function TYPE()
  579.  
  580.  
  581.     /**
  582.      *    Convert a multi-dimensional array to a simple 1-dimensional array
  583.      *
  584.      *    @param    array    $array    Array to be flattened
  585.      *    @return    array    Flattened array
  586.      */
  587.     public static function flattenArray($array) {
  588.         if (!is_array($array)) {
  589.             return (array) $array;
  590.         }
  591.  
  592.         $arrayValues = array();
  593.         foreach ($array as $value) {
  594.             if (is_array($value)) {
  595.                 foreach ($value as $val) {
  596.                     if (is_array($val)) {
  597.                         foreach ($val as $v) {
  598.                             $arrayValues[] = $v;
  599.                         }
  600.                     } else {
  601.                         $arrayValues[] = $val;
  602.                     }
  603.                 }
  604.             } else {
  605.                 $arrayValues[] = $value;
  606.             }
  607.         }
  608.  
  609.         return $arrayValues;
  610.     }    //    function flattenArray()
  611.  
  612.  
  613.     /**
  614.      *    Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing
  615.      *
  616.      *    @param    array    $array    Array to be flattened
  617.      *    @return    array    Flattened array
  618.      */
  619.     public static function flattenArrayIndexed($array) {
  620.         if (!is_array($array)) {
  621.             return (array) $array;
  622.         }
  623.  
  624.         $arrayValues = array();
  625.         foreach ($array as $k1 => $value) {
  626.             if (is_array($value)) {
  627.                 foreach ($value as $k2 => $val) {
  628.                     if (is_array($val)) {
  629.                         foreach ($val as $k3 => $v) {
  630.                             $arrayValues[$k1.'.'.$k2.'.'.$k3] = $v;
  631.                         }
  632.                     } else {
  633.                         $arrayValues[$k1.'.'.$k2] = $val;
  634.                     }
  635.                 }
  636.             } else {
  637.                 $arrayValues[$k1] = $value;
  638.             }
  639.         }
  640.  
  641.         return $arrayValues;
  642.     }    //    function flattenArrayIndexed()
  643.  
  644.  
  645.     /**
  646.      *    Convert an array to a single scalar value by extracting the first element
  647.      *
  648.      *    @param    mixed        $value        Array or scalar value
  649.      *    @return    mixed 
  650.      */
  651.     public static function flattenSingleValue($value = '') {
  652.         while (is_array($value)) {
  653.             $value = array_pop($value);
  654.         }
  655.  
  656.         return $value;
  657.     }    //    function flattenSingleValue()
  658.  
  659. }    //    class PHPExcel_Calculation_Functions
  660.  
  661.  
  662. //
  663. //    There are a few mathematical functions that aren't available on all versions of PHP for all platforms
  664. //    These functions aren't available in Windows implementations of PHP prior to version 5.3.0
  665. //    So we test if they do exist for this version of PHP/operating platform; and if not we create them
  666. //
  667. if (!function_exists('acosh')) {
  668.     function acosh($x) {
  669.         return 2 * log(sqrt(($x + 1) / 2) + sqrt(($x - 1) / 2));
  670.     }    //    function acosh()
  671. }
  672.  
  673. if (!function_exists('asinh')) {
  674.     function asinh($x) {
  675.         return log($x + sqrt(1 + $x * $x));
  676.     }    //    function asinh()
  677. }
  678.  
  679. if (!function_exists('atanh')) {
  680.     function atanh($x) {
  681.         return (log(1 + $x) - log(1 - $x)) / 2;
  682.     }    //    function atanh()
  683. }
  684.  
  685. if (!function_exists('money_format')) {
  686.     function money_format($format, $number) {
  687.         $regex = array( '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?(?:#([0-9]+))?',
  688.                          '(?:\.([0-9]+))?([in%])/'
  689.                       );
  690.         $regex = implode('', $regex);
  691.         if (setlocale(LC_MONETARY, null) == '') {
  692.             setlocale(LC_MONETARY, '');
  693.         }
  694.         $locale = localeconv();
  695.         $number = floatval($number);
  696.         if (!preg_match($regex, $format, $fmatch)) {
  697.             trigger_error("No format specified or invalid format", E_USER_WARNING);
  698.             return $number;
  699.         }
  700.         $flags = array( 'fillchar'    => preg_match('/\=(.)/', $fmatch[1], $match) ? $match[1] : ' ',
  701.                         'nogroup'    => preg_match('/\^/', $fmatch[1]) > 0,
  702.                         'usesignal'    => preg_match('/\+|\(/', $fmatch[1], $match) ? $match[0] : '+',
  703.                         'nosimbol'    => preg_match('/\!/', $fmatch[1]) > 0,
  704.                         'isleft'    => preg_match('/\-/', $fmatch[1]) > 0
  705.                       );
  706.         $width    = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
  707.         $left    = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
  708.         $right    = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
  709.         $conversion = $fmatch[5];
  710.         $positive = true;
  711.         if ($number < 0) {
  712.             $positive = false;
  713.             $number *= -1;
  714.         }
  715.         $letter = $positive ? 'p' : 'n';
  716.         $prefix = $suffix = $cprefix = $csuffix = $signal = '';
  717.         if (!$positive) {
  718.             $signal = $locale['negative_sign'];
  719.             switch (true) {
  720.                 case $locale['n_sign_posn'] == 0 || $flags['usesignal'] == '(':
  721.                     $prefix = '(';
  722.                     $suffix = ')';
  723.                     break;
  724.                 case $locale['n_sign_posn'] == 1:
  725.                     $prefix = $signal;
  726.                     break;
  727.                 case $locale['n_sign_posn'] == 2:
  728.                     $suffix = $signal;
  729.                     break;
  730.                 case $locale['n_sign_posn'] == 3:
  731.                     $cprefix = $signal;
  732.                     break;
  733.                 case $locale['n_sign_posn'] == 4:
  734.                     $csuffix = $signal;
  735.                     break;
  736.             }
  737.         }
  738.         if (!$flags['nosimbol']) {
  739.             $currency = $cprefix;
  740.             $currency .= ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']);
  741.             $currency .= $csuffix;
  742.             $currency = iconv('ISO-8859-1','UTF-8',$currency);
  743.         } else {
  744.             $currency = '';
  745.         }
  746.         $space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
  747.  
  748.         $number = number_format($number, $right, $locale['mon_decimal_point'], $flags['nogroup'] ? '' : $locale['mon_thousands_sep'] );
  749.         $number = explode($locale['mon_decimal_point'], $number);
  750.  
  751.         $n = strlen($prefix) + strlen($currency);
  752.         if ($left > 0 && $left > $n) {
  753.             if ($flags['isleft']) {
  754.                 $number[0] .= str_repeat($flags['fillchar'], $left - $n);
  755.             } else {
  756.                 $number[0] = str_repeat($flags['fillchar'], $left - $n) . $number[0];
  757.             }
  758.         }
  759.         $number = implode($locale['mon_decimal_point'], $number);
  760.         if ($locale["{$letter}_cs_precedes"]) {
  761.             $number = $prefix . $currency . $space . $number . $suffix;
  762.         } else {
  763.             $number = $prefix . $number . $space . $currency . $suffix;
  764.         }
  765.         if ($width > 0) {
  766.             $number = str_pad($number, $width, $flags['fillchar'], $flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT);
  767.         }
  768.         $format = str_replace($fmatch[0], $number, $format);
  769.         return $format;
  770.     }    //    function money_format()
  771. }
  772.  
  773.  
  774. //
  775. //    Strangely, PHP doesn't have a mb_str_replace multibyte function
  776. //    As we'll only ever use this function with UTF-8 characters, we can simply "hard-code" the character set
  777. //
  778. if ((!function_exists('mb_str_replace')) &&
  779.     (function_exists('mb_substr')) && (function_exists('mb_strlen')) && (function_exists('mb_strpos'))) {
  780.     function mb_str_replace($search, $replace, $subject) {
  781.         if(is_array($subject)) {
  782.             $ret = array();
  783.             foreach($subject as $key => $val) {
  784.                 $ret[$key] = mb_str_replace($search, $replace, $val);
  785.             }
  786.             return $ret;
  787.         }
  788.  
  789.         foreach((array) $search as $key => $s) {
  790.             if($s == '') {
  791.                 continue;
  792.             }
  793.             $r = !is_array($replace) ? $replace : (array_key_exists($key, $replace) ? $replace[$key] : '');
  794.             $pos = mb_strpos($subject, $s, 0, 'UTF-8');
  795.             while($pos !== false) {
  796.                 $subject = mb_substr($subject, 0, $pos, 'UTF-8') . $r . mb_substr($subject, $pos + mb_strlen($s, 'UTF-8'), 65535, 'UTF-8');
  797.                 $pos = mb_strpos($subject, $s, $pos + mb_strlen($r, 'UTF-8'), 'UTF-8');
  798.             }
  799.         }
  800.         return $subject;
  801.     }
  802. }

Documentation generated on Sun, 27 Feb 2011 16:32:08 -0800 by phpDocumentor 1.4.3