TextData.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-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. /**
  36. * PHPExcel_Calculation_TextData
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Calculation
  40. * @copyright Copyright (c) 2006 - 2014 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 $stringValue 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 $stringValue 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 $characters 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. $mask = '$#,##0';
  183. if ($decimals > 0) {
  184. $mask .= '.' . str_repeat('0',$decimals);
  185. } else {
  186. $round = pow(10,abs($decimals));
  187. if ($value < 0) { $round = 0-$round; }
  188. $value = PHPExcel_Calculation_MathTrig::MROUND($value, $round);
  189. }
  190. return PHPExcel_Style_NumberFormat::toFormattedString($value, $mask);
  191. } // function DOLLAR()
  192. /**
  193. * SEARCHSENSITIVE
  194. *
  195. * @param string $needle The string to look for
  196. * @param string $haystack The string in which to look
  197. * @param int $offset Offset within $haystack
  198. * @return string
  199. */
  200. public static function SEARCHSENSITIVE($needle,$haystack,$offset=1) {
  201. $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle);
  202. $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack);
  203. $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset);
  204. if (!is_bool($needle)) {
  205. if (is_bool($haystack)) {
  206. $haystack = ($haystack) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  207. }
  208. if (($offset > 0) && (PHPExcel_Shared_String::CountCharacters($haystack) > $offset)) {
  209. if (PHPExcel_Shared_String::CountCharacters($needle) == 0) {
  210. return $offset;
  211. }
  212. if (function_exists('mb_strpos')) {
  213. $pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
  214. } else {
  215. $pos = strpos($haystack, $needle, --$offset);
  216. }
  217. if ($pos !== false) {
  218. return ++$pos;
  219. }
  220. }
  221. }
  222. return PHPExcel_Calculation_Functions::VALUE();
  223. } // function SEARCHSENSITIVE()
  224. /**
  225. * SEARCHINSENSITIVE
  226. *
  227. * @param string $needle The string to look for
  228. * @param string $haystack The string in which to look
  229. * @param int $offset Offset within $haystack
  230. * @return string
  231. */
  232. public static function SEARCHINSENSITIVE($needle,$haystack,$offset=1) {
  233. $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle);
  234. $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack);
  235. $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset);
  236. if (!is_bool($needle)) {
  237. if (is_bool($haystack)) {
  238. $haystack = ($haystack) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  239. }
  240. if (($offset > 0) && (PHPExcel_Shared_String::CountCharacters($haystack) > $offset)) {
  241. if (PHPExcel_Shared_String::CountCharacters($needle) == 0) {
  242. return $offset;
  243. }
  244. if (function_exists('mb_stripos')) {
  245. $pos = mb_stripos($haystack, $needle, --$offset,'UTF-8');
  246. } else {
  247. $pos = stripos($haystack, $needle, --$offset);
  248. }
  249. if ($pos !== false) {
  250. return ++$pos;
  251. }
  252. }
  253. }
  254. return PHPExcel_Calculation_Functions::VALUE();
  255. } // function SEARCHINSENSITIVE()
  256. /**
  257. * FIXEDFORMAT
  258. *
  259. * @param mixed $value Value to check
  260. * @param integer $decimals
  261. * @param boolean $no_commas
  262. * @return boolean
  263. */
  264. public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = FALSE) {
  265. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  266. $decimals = PHPExcel_Calculation_Functions::flattenSingleValue($decimals);
  267. $no_commas = PHPExcel_Calculation_Functions::flattenSingleValue($no_commas);
  268. // Validate parameters
  269. if (!is_numeric($value) || !is_numeric($decimals)) {
  270. return PHPExcel_Calculation_Functions::NaN();
  271. }
  272. $decimals = floor($decimals);
  273. $valueResult = round($value,$decimals);
  274. if ($decimals < 0) { $decimals = 0; }
  275. if (!$no_commas) {
  276. $valueResult = number_format($valueResult,$decimals);
  277. }
  278. return (string) $valueResult;
  279. } // function FIXEDFORMAT()
  280. /**
  281. * LEFT
  282. *
  283. * @param string $value Value
  284. * @param int $chars Number of characters
  285. * @return string
  286. */
  287. public static function LEFT($value = '', $chars = 1) {
  288. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  289. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  290. if ($chars < 0) {
  291. return PHPExcel_Calculation_Functions::VALUE();
  292. }
  293. if (is_bool($value)) {
  294. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  295. }
  296. if (function_exists('mb_substr')) {
  297. return mb_substr($value, 0, $chars, 'UTF-8');
  298. } else {
  299. return substr($value, 0, $chars);
  300. }
  301. } // function LEFT()
  302. /**
  303. * MID
  304. *
  305. * @param string $value Value
  306. * @param int $start Start character
  307. * @param int $chars Number of characters
  308. * @return string
  309. */
  310. public static function MID($value = '', $start = 1, $chars = null) {
  311. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  312. $start = PHPExcel_Calculation_Functions::flattenSingleValue($start);
  313. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  314. if (($start < 1) || ($chars < 0)) {
  315. return PHPExcel_Calculation_Functions::VALUE();
  316. }
  317. if (is_bool($value)) {
  318. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  319. }
  320. if (function_exists('mb_substr')) {
  321. return mb_substr($value, --$start, $chars, 'UTF-8');
  322. } else {
  323. return substr($value, --$start, $chars);
  324. }
  325. } // function MID()
  326. /**
  327. * RIGHT
  328. *
  329. * @param string $value Value
  330. * @param int $chars Number of characters
  331. * @return string
  332. */
  333. public static function RIGHT($value = '', $chars = 1) {
  334. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  335. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  336. if ($chars < 0) {
  337. return PHPExcel_Calculation_Functions::VALUE();
  338. }
  339. if (is_bool($value)) {
  340. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  341. }
  342. if ((function_exists('mb_substr')) && (function_exists('mb_strlen'))) {
  343. return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
  344. } else {
  345. return substr($value, strlen($value) - $chars);
  346. }
  347. } // function RIGHT()
  348. /**
  349. * STRINGLENGTH
  350. *
  351. * @param string $value Value
  352. * @return string
  353. */
  354. public static function STRINGLENGTH($value = '') {
  355. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  356. if (is_bool($value)) {
  357. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  358. }
  359. if (function_exists('mb_strlen')) {
  360. return mb_strlen($value, 'UTF-8');
  361. } else {
  362. return strlen($value);
  363. }
  364. } // function STRINGLENGTH()
  365. /**
  366. * LOWERCASE
  367. *
  368. * Converts a string value to upper case.
  369. *
  370. * @param string $mixedCaseString
  371. * @return string
  372. */
  373. public static function LOWERCASE($mixedCaseString) {
  374. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  375. if (is_bool($mixedCaseString)) {
  376. $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  377. }
  378. return PHPExcel_Shared_String::StrToLower($mixedCaseString);
  379. } // function LOWERCASE()
  380. /**
  381. * UPPERCASE
  382. *
  383. * Converts a string value to upper case.
  384. *
  385. * @param string $mixedCaseString
  386. * @return string
  387. */
  388. public static function UPPERCASE($mixedCaseString) {
  389. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  390. if (is_bool($mixedCaseString)) {
  391. $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  392. }
  393. return PHPExcel_Shared_String::StrToUpper($mixedCaseString);
  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) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  407. }
  408. return PHPExcel_Shared_String::StrToTitle($mixedCaseString);
  409. } // function PROPERCASE()
  410. /**
  411. * REPLACE
  412. *
  413. * @param string $oldText String to modify
  414. * @param int $start Start character
  415. * @param int $chars Number of characters
  416. * @param string $newText String to replace in defined position
  417. * @return string
  418. */
  419. public static function REPLACE($oldText = '', $start = 1, $chars = null, $newText) {
  420. $oldText = PHPExcel_Calculation_Functions::flattenSingleValue($oldText);
  421. $start = PHPExcel_Calculation_Functions::flattenSingleValue($start);
  422. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  423. $newText = PHPExcel_Calculation_Functions::flattenSingleValue($newText);
  424. $left = self::LEFT($oldText,$start-1);
  425. $right = self::RIGHT($oldText,self::STRINGLENGTH($oldText)-($start+$chars)+1);
  426. return $left.$newText.$right;
  427. } // function REPLACE()
  428. /**
  429. * SUBSTITUTE
  430. *
  431. * @param string $text Value
  432. * @param string $fromText From Value
  433. * @param string $toText To Value
  434. * @param integer $instance Instance Number
  435. * @return string
  436. */
  437. public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) {
  438. $text = PHPExcel_Calculation_Functions::flattenSingleValue($text);
  439. $fromText = PHPExcel_Calculation_Functions::flattenSingleValue($fromText);
  440. $toText = PHPExcel_Calculation_Functions::flattenSingleValue($toText);
  441. $instance = floor(PHPExcel_Calculation_Functions::flattenSingleValue($instance));
  442. if ($instance == 0) {
  443. if(function_exists('mb_str_replace')) {
  444. return mb_str_replace($fromText,$toText,$text);
  445. } else {
  446. return str_replace($fromText,$toText,$text);
  447. }
  448. } else {
  449. $pos = -1;
  450. while($instance > 0) {
  451. if (function_exists('mb_strpos')) {
  452. $pos = mb_strpos($text, $fromText, $pos+1, 'UTF-8');
  453. } else {
  454. $pos = strpos($text, $fromText, $pos+1);
  455. }
  456. if ($pos === false) {
  457. break;
  458. }
  459. --$instance;
  460. }
  461. if ($pos !== false) {
  462. if (function_exists('mb_strlen')) {
  463. return self::REPLACE($text,++$pos,mb_strlen($fromText, 'UTF-8'),$toText);
  464. } else {
  465. return self::REPLACE($text,++$pos,strlen($fromText),$toText);
  466. }
  467. }
  468. }
  469. return $text;
  470. } // function SUBSTITUTE()
  471. /**
  472. * RETURNSTRING
  473. *
  474. * @param mixed $testValue Value to check
  475. * @return boolean
  476. */
  477. public static function RETURNSTRING($testValue = '') {
  478. $testValue = PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
  479. if (is_string($testValue)) {
  480. return $testValue;
  481. }
  482. return Null;
  483. } // function RETURNSTRING()
  484. /**
  485. * TEXTFORMAT
  486. *
  487. * @param mixed $value Value to check
  488. * @param string $format Format mask to use
  489. * @return boolean
  490. */
  491. public static function TEXTFORMAT($value,$format) {
  492. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  493. $format = PHPExcel_Calculation_Functions::flattenSingleValue($format);
  494. if ((is_string($value)) && (!is_numeric($value)) && PHPExcel_Shared_Date::isDateTimeFormatCode($format)) {
  495. $value = PHPExcel_Calculation_DateTime::DATEVALUE($value);
  496. }
  497. return (string) PHPExcel_Style_NumberFormat::toFormattedString($value,$format);
  498. } // function TEXTFORMAT()
  499. } // class PHPExcel_Calculation_TextData