PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/phpexcel/PHPExcel/Calculation/TextData.php

https://bitbucket.org/speedealing/speedealing
PHP | 588 lines | 314 code | 87 blank | 187 comment | 105 complexity | 4f86f9f1888f60eeb6614e3660aaae81 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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. /** 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. /**
  36. * PHPExcel_Calculation_TextData
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Calculation
  40. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Calculation_TextData {
  43. private static $_invalidChars = Null;
  44. private static function _uniord($c) {
  45. if (ord($c{0}) >=0 && ord($c{0}) <= 127)
  46. return ord($c{0});
  47. if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
  48. return (ord($c{0})-192)*64 + (ord($c{1})-128);
  49. if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
  50. return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
  51. if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
  52. return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
  53. if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
  54. return (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
  55. if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
  56. return (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
  57. if (ord($c{0}) >= 254 && ord($c{0}) <= 255) //error
  58. return PHPExcel_Calculation_Functions::VALUE();
  59. return 0;
  60. } // function _uniord()
  61. /**
  62. * CHARACTER
  63. *
  64. * @param string $character Value
  65. * @return int
  66. */
  67. public static function CHARACTER($character) {
  68. $character = PHPExcel_Calculation_Functions::flattenSingleValue($character);
  69. if ((!is_numeric($character)) || ($character < 0)) {
  70. return PHPExcel_Calculation_Functions::VALUE();
  71. }
  72. if (function_exists('mb_convert_encoding')) {
  73. return mb_convert_encoding('&#'.intval($character).';', 'UTF-8', 'HTML-ENTITIES');
  74. } else {
  75. return chr(intval($character));
  76. }
  77. }
  78. /**
  79. * TRIMNONPRINTABLE
  80. *
  81. * @param mixed $value Value to check
  82. * @return string
  83. */
  84. public static function TRIMNONPRINTABLE($stringValue = '') {
  85. $stringValue = PHPExcel_Calculation_Functions::flattenSingleValue($stringValue);
  86. if (is_bool($stringValue)) {
  87. $stringValue = ($stringValue) ? 'TRUE' : 'FALSE';
  88. }
  89. if (self::$_invalidChars == Null) {
  90. self::$_invalidChars = range(chr(0),chr(31));
  91. }
  92. if (is_string($stringValue) || is_numeric($stringValue)) {
  93. return str_replace(self::$_invalidChars,'',trim($stringValue,"\x00..\x1F"));
  94. }
  95. return Null;
  96. } // function TRIMNONPRINTABLE()
  97. /**
  98. * TRIMSPACES
  99. *
  100. * @param mixed $value Value to check
  101. * @return string
  102. */
  103. public static function TRIMSPACES($stringValue = '') {
  104. $stringValue = PHPExcel_Calculation_Functions::flattenSingleValue($stringValue);
  105. if (is_string($stringValue) || is_numeric($stringValue)) {
  106. return trim(preg_replace('/ +/',' ',$stringValue));
  107. }
  108. return Null;
  109. } // function TRIMSPACES()
  110. /**
  111. * ASCIICODE
  112. *
  113. * @param string $character Value
  114. * @return int
  115. */
  116. public static function ASCIICODE($characters) {
  117. $characters = PHPExcel_Calculation_Functions::flattenSingleValue($characters);
  118. if (is_bool($characters)) {
  119. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  120. $characters = (int) $characters;
  121. } else {
  122. if ($characters) {
  123. $characters = 'True';
  124. } else {
  125. $characters = 'False';
  126. }
  127. }
  128. }
  129. $character = $characters;
  130. if ((function_exists('mb_strlen')) && (function_exists('mb_substr'))) {
  131. if (mb_strlen($characters, 'UTF-8') > 1) { $character = mb_substr($characters, 0, 1, 'UTF-8'); }
  132. return self::_uniord($character);
  133. } else {
  134. if (strlen($characters) > 0) { $character = substr($characters, 0, 1); }
  135. return ord($character);
  136. }
  137. } // function ASCIICODE()
  138. /**
  139. * CONCATENATE
  140. *
  141. * @return string
  142. */
  143. public static function CONCATENATE() {
  144. // Return value
  145. $returnValue = '';
  146. // Loop through arguments
  147. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  148. foreach ($aArgs as $arg) {
  149. if (is_bool($arg)) {
  150. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  151. $arg = (int) $arg;
  152. } else {
  153. if ($arg) {
  154. $arg = 'TRUE';
  155. } else {
  156. $arg = 'FALSE';
  157. }
  158. }
  159. }
  160. $returnValue .= $arg;
  161. }
  162. // Return
  163. return $returnValue;
  164. } // function CONCATENATE()
  165. /**
  166. * DOLLAR
  167. *
  168. * This function converts a number to text using currency format, with the decimals rounded to the specified place.
  169. * The format used is $#,##0.00_);($#,##0.00)..
  170. *
  171. * @param float $value The value to format
  172. * @param int $decimals The number of digits to display to the right of the decimal point.
  173. * If decimals is negative, number is rounded to the left of the decimal point.
  174. * If you omit decimals, it is assumed to be 2
  175. * @return string
  176. */
  177. public static function DOLLAR($value = 0, $decimals = 2) {
  178. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  179. $decimals = is_null($decimals) ? 0 : PHPExcel_Calculation_Functions::flattenSingleValue($decimals);
  180. // Validate parameters
  181. if (!is_numeric($value) || !is_numeric($decimals)) {
  182. return PHPExcel_Calculation_Functions::NaN();
  183. }
  184. $decimals = floor($decimals);
  185. if ($decimals > 0) {
  186. return money_format('%.'.$decimals.'n',$value);
  187. } else {
  188. $round = pow(10,abs($decimals));
  189. if ($value < 0) { $round = 0-$round; }
  190. $value = PHPExcel_Calculation_MathTrig::MROUND($value,$round);
  191. // The implementation of money_format used if the standard PHP function is not available can't handle decimal places of 0,
  192. // so we display to 1 dp and chop off that character and the decimal separator using substr
  193. return substr(money_format('%.1n',$value),0,-2);
  194. }
  195. } // function DOLLAR()
  196. /**
  197. * SEARCHSENSITIVE
  198. *
  199. * @param string $needle The string to look for
  200. * @param string $haystack The string in which to look
  201. * @param int $offset Offset within $haystack
  202. * @return string
  203. */
  204. public static function SEARCHSENSITIVE($needle,$haystack,$offset=1) {
  205. $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle);
  206. $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack);
  207. $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset);
  208. if (!is_bool($needle)) {
  209. if (is_bool($haystack)) {
  210. $haystack = ($haystack) ? 'TRUE' : 'FALSE';
  211. }
  212. if (($offset > 0) && (strlen($haystack) > $offset)) {
  213. if (function_exists('mb_strpos')) {
  214. $pos = mb_strpos($haystack, $needle, --$offset,'UTF-8');
  215. } else {
  216. $pos = strpos($haystack, $needle, --$offset);
  217. }
  218. if ($pos !== false) {
  219. return ++$pos;
  220. }
  221. }
  222. }
  223. return PHPExcel_Calculation_Functions::VALUE();
  224. } // function SEARCHSENSITIVE()
  225. /**
  226. * SEARCHINSENSITIVE
  227. *
  228. * @param string $needle The string to look for
  229. * @param string $haystack The string in which to look
  230. * @param int $offset Offset within $haystack
  231. * @return string
  232. */
  233. public static function SEARCHINSENSITIVE($needle,$haystack,$offset=1) {
  234. $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle);
  235. $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack);
  236. $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset);
  237. if (!is_bool($needle)) {
  238. if (is_bool($haystack)) {
  239. $haystack = ($haystack) ? 'TRUE' : 'FALSE';
  240. }
  241. if (($offset > 0) && (strlen($haystack) > $offset)) {
  242. if (function_exists('mb_stripos')) {
  243. $pos = mb_stripos($haystack, $needle, --$offset,'UTF-8');
  244. } else {
  245. $pos = stripos($haystack, $needle, --$offset);
  246. }
  247. if ($pos !== false) {
  248. return ++$pos;
  249. }
  250. }
  251. }
  252. return PHPExcel_Calculation_Functions::VALUE();
  253. } // function SEARCHINSENSITIVE()
  254. /**
  255. * FIXEDFORMAT
  256. *
  257. * @param mixed $value Value to check
  258. * @return boolean
  259. */
  260. public static function FIXEDFORMAT($value,$decimals=2,$no_commas=false) {
  261. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  262. $decimals = PHPExcel_Calculation_Functions::flattenSingleValue($decimals);
  263. $no_commas = PHPExcel_Calculation_Functions::flattenSingleValue($no_commas);
  264. $valueResult = round($value,$decimals);
  265. if ($decimals < 0) { $decimals = 0; }
  266. if (!$no_commas) {
  267. $valueResult = number_format($valueResult,$decimals);
  268. }
  269. return (string) $valueResult;
  270. } // function FIXEDFORMAT()
  271. /**
  272. * LEFT
  273. *
  274. * @param string $value Value
  275. * @param int $chars Number of characters
  276. * @return string
  277. */
  278. public static function LEFT($value = '', $chars = 1) {
  279. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  280. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  281. if ($chars < 0) {
  282. return PHPExcel_Calculation_Functions::VALUE();
  283. }
  284. if (is_bool($value)) {
  285. $value = ($value) ? 'TRUE' : 'FALSE';
  286. }
  287. if (function_exists('mb_substr')) {
  288. return mb_substr($value, 0, $chars, 'UTF-8');
  289. } else {
  290. return substr($value, 0, $chars);
  291. }
  292. } // function LEFT()
  293. /**
  294. * MID
  295. *
  296. * @param string $value Value
  297. * @param int $start Start character
  298. * @param int $chars Number of characters
  299. * @return string
  300. */
  301. public static function MID($value = '', $start = 1, $chars = null) {
  302. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  303. $start = PHPExcel_Calculation_Functions::flattenSingleValue($start);
  304. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  305. if (($start < 1) || ($chars < 0)) {
  306. return PHPExcel_Calculation_Functions::VALUE();
  307. }
  308. if (is_bool($value)) {
  309. $value = ($value) ? 'TRUE' : 'FALSE';
  310. }
  311. if (function_exists('mb_substr')) {
  312. return mb_substr($value, --$start, $chars, 'UTF-8');
  313. } else {
  314. return substr($value, --$start, $chars);
  315. }
  316. } // function MID()
  317. /**
  318. * RIGHT
  319. *
  320. * @param string $value Value
  321. * @param int $chars Number of characters
  322. * @return string
  323. */
  324. public static function RIGHT($value = '', $chars = 1) {
  325. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  326. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  327. if ($chars < 0) {
  328. return PHPExcel_Calculation_Functions::VALUE();
  329. }
  330. if (is_bool($value)) {
  331. $value = ($value) ? 'TRUE' : 'FALSE';
  332. }
  333. if ((function_exists('mb_substr')) && (function_exists('mb_strlen'))) {
  334. return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
  335. } else {
  336. return substr($value, strlen($value) - $chars);
  337. }
  338. } // function RIGHT()
  339. /**
  340. * STRINGLENGTH
  341. *
  342. * @param string $value Value
  343. * @param int $chars Number of characters
  344. * @return string
  345. */
  346. public static function STRINGLENGTH($value = '') {
  347. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  348. if (is_bool($value)) {
  349. $value = ($value) ? 'TRUE' : 'FALSE';
  350. }
  351. if (function_exists('mb_strlen')) {
  352. return mb_strlen($value, 'UTF-8');
  353. } else {
  354. return strlen($value);
  355. }
  356. } // function STRINGLENGTH()
  357. /**
  358. * LOWERCASE
  359. *
  360. * Converts a string value to upper case.
  361. *
  362. * @param string $mixedCaseString
  363. * @return string
  364. */
  365. public static function LOWERCASE($mixedCaseString) {
  366. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  367. if (is_bool($mixedCaseString)) {
  368. $mixedCaseString = ($mixedCaseString) ? 'TRUE' : 'FALSE';
  369. }
  370. if (function_exists('mb_convert_case')) {
  371. return mb_convert_case($mixedCaseString, MB_CASE_LOWER, 'UTF-8');
  372. } else {
  373. return strtoupper($mixedCaseString);
  374. }
  375. } // function LOWERCASE()
  376. /**
  377. * UPPERCASE
  378. *
  379. * Converts a string value to upper case.
  380. *
  381. * @param string $mixedCaseString
  382. * @return string
  383. */
  384. public static function UPPERCASE($mixedCaseString) {
  385. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  386. if (is_bool($mixedCaseString)) {
  387. $mixedCaseString = ($mixedCaseString) ? 'TRUE' : 'FALSE';
  388. }
  389. if (function_exists('mb_convert_case')) {
  390. return mb_convert_case($mixedCaseString, MB_CASE_UPPER, 'UTF-8');
  391. } else {
  392. return strtoupper($mixedCaseString);
  393. }
  394. } // function UPPERCASE()
  395. /**
  396. * PROPERCASE
  397. *
  398. * Converts a string value to upper case.
  399. *
  400. * @param string $mixedCaseString
  401. * @return string
  402. */
  403. public static function PROPERCASE($mixedCaseString) {
  404. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  405. if (is_bool($mixedCaseString)) {
  406. $mixedCaseString = ($mixedCaseString) ? 'TRUE' : 'FALSE';
  407. }
  408. if (function_exists('mb_convert_case')) {
  409. return mb_convert_case($mixedCaseString, MB_CASE_TITLE, 'UTF-8');
  410. } else {
  411. return ucwords($mixedCaseString);
  412. }
  413. } // function PROPERCASE()
  414. /**
  415. * REPLACE
  416. *
  417. * @param string $value Value
  418. * @param int $start Start character
  419. * @param int $chars Number of characters
  420. * @return string
  421. */
  422. public static function REPLACE($oldText = '', $start = 1, $chars = null, $newText) {
  423. $oldText = PHPExcel_Calculation_Functions::flattenSingleValue($oldText);
  424. $start = PHPExcel_Calculation_Functions::flattenSingleValue($start);
  425. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  426. $newText = PHPExcel_Calculation_Functions::flattenSingleValue($newText);
  427. $left = self::LEFT($oldText,$start-1);
  428. $right = self::RIGHT($oldText,self::STRINGLENGTH($oldText)-($start+$chars)+1);
  429. return $left.$newText.$right;
  430. } // function REPLACE()
  431. /**
  432. * SUBSTITUTE
  433. *
  434. * @param string $text Value
  435. * @param string $fromText From Value
  436. * @param string $toText To Value
  437. * @param integer $instance Instance Number
  438. * @return string
  439. */
  440. public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) {
  441. $text = PHPExcel_Calculation_Functions::flattenSingleValue($text);
  442. $fromText = PHPExcel_Calculation_Functions::flattenSingleValue($fromText);
  443. $toText = PHPExcel_Calculation_Functions::flattenSingleValue($toText);
  444. $instance = floor(PHPExcel_Calculation_Functions::flattenSingleValue($instance));
  445. if ($instance == 0) {
  446. if(function_exists('mb_str_replace')) {
  447. return mb_str_replace($fromText,$toText,$text);
  448. } else {
  449. return str_replace($fromText,$toText,$text);
  450. }
  451. } else {
  452. $pos = -1;
  453. while($instance > 0) {
  454. if (function_exists('mb_strpos')) {
  455. $pos = mb_strpos($text, $fromText, $pos+1, 'UTF-8');
  456. } else {
  457. $pos = strpos($text, $fromText, $pos+1);
  458. }
  459. if ($pos === false) {
  460. break;
  461. }
  462. --$instance;
  463. }
  464. if ($pos !== false) {
  465. if (function_exists('mb_strlen')) {
  466. return self::REPLACE($text,++$pos,mb_strlen($fromText, 'UTF-8'),$toText);
  467. } else {
  468. return self::REPLACE($text,++$pos,strlen($fromText),$toText);
  469. }
  470. }
  471. }
  472. return $left.$newText.$right;
  473. } // function SUBSTITUTE()
  474. /**
  475. * RETURNSTRING
  476. *
  477. * @param mixed $value Value to check
  478. * @return boolean
  479. */
  480. public static function RETURNSTRING($testValue = '') {
  481. $testValue = PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
  482. if (is_string($testValue)) {
  483. return $testValue;
  484. }
  485. return Null;
  486. } // function RETURNSTRING()
  487. /**
  488. * TEXTFORMAT
  489. *
  490. * @param mixed $value Value to check
  491. * @return boolean
  492. */
  493. public static function TEXTFORMAT($value,$format) {
  494. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  495. $format = PHPExcel_Calculation_Functions::flattenSingleValue($format);
  496. if ((is_string($value)) && (!is_numeric($value)) && PHPExcel_Shared_Date::isDateTimeFormatCode($format)) {
  497. $value = PHPExcel_Calculation_DateTime::DATEVALUE($value);
  498. }
  499. return (string) PHPExcel_Style_NumberFormat::toFormattedString($value,$format);
  500. } // function TEXTFORMAT()
  501. } // class PHPExcel_Calculation_TextData