PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/PHPExcel/Classes/PHPExcel/Calculation/TextData.php

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