Functions.php 20 KB

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