TextData.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. /**
  36. * PHPExcel_Calculation_TextData
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Calculation
  40. * @copyright Copyright (c) 2006 - 2013 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. 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) && (PHPExcel_Shared_String::CountCharacters($haystack) > $offset)) {
  210. if (PHPExcel_Shared_String::CountCharacters($needle) == 0) {
  211. return $offset;
  212. }
  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) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  240. }
  241. if (($offset > 0) && (PHPExcel_Shared_String::CountCharacters($haystack) > $offset)) {
  242. if (PHPExcel_Shared_String::CountCharacters($needle) == 0) {
  243. return $offset;
  244. }
  245. if (function_exists('mb_stripos')) {
  246. $pos = mb_stripos($haystack, $needle, --$offset,'UTF-8');
  247. } else {
  248. $pos = stripos($haystack, $needle, --$offset);
  249. }
  250. if ($pos !== false) {
  251. return ++$pos;
  252. }
  253. }
  254. }
  255. return PHPExcel_Calculation_Functions::VALUE();
  256. } // function SEARCHINSENSITIVE()
  257. /**
  258. * FIXEDFORMAT
  259. *
  260. * @param mixed $value Value to check
  261. * @param integer $decimals
  262. * @param boolean $no_commas
  263. * @return boolean
  264. */
  265. public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = FALSE) {
  266. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  267. $decimals = PHPExcel_Calculation_Functions::flattenSingleValue($decimals);
  268. $no_commas = PHPExcel_Calculation_Functions::flattenSingleValue($no_commas);
  269. // Validate parameters
  270. if (!is_numeric($value) || !is_numeric($decimals)) {
  271. return PHPExcel_Calculation_Functions::NaN();
  272. }
  273. $decimals = floor($decimals);
  274. $valueResult = round($value,$decimals);
  275. if ($decimals < 0) { $decimals = 0; }
  276. if (!$no_commas) {
  277. $valueResult = number_format($valueResult,$decimals);
  278. }
  279. return (string) $valueResult;
  280. } // function FIXEDFORMAT()
  281. /**
  282. * LEFT
  283. *
  284. * @param string $value Value
  285. * @param int $chars Number of characters
  286. * @return string
  287. */
  288. public static function LEFT($value = '', $chars = 1) {
  289. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  290. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  291. if ($chars < 0) {
  292. return PHPExcel_Calculation_Functions::VALUE();
  293. }
  294. if (is_bool($value)) {
  295. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  296. }
  297. if (function_exists('mb_substr')) {
  298. return mb_substr($value, 0, $chars, 'UTF-8');
  299. } else {
  300. return substr($value, 0, $chars);
  301. }
  302. } // function LEFT()
  303. /**
  304. * MID
  305. *
  306. * @param string $value Value
  307. * @param int $start Start character
  308. * @param int $chars Number of characters
  309. * @return string
  310. */
  311. public static function MID($value = '', $start = 1, $chars = null) {
  312. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  313. $start = PHPExcel_Calculation_Functions::flattenSingleValue($start);
  314. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  315. if (($start < 1) || ($chars < 0)) {
  316. return PHPExcel_Calculation_Functions::VALUE();
  317. }
  318. if (is_bool($value)) {
  319. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  320. }
  321. if (function_exists('mb_substr')) {
  322. return mb_substr($value, --$start, $chars, 'UTF-8');
  323. } else {
  324. return substr($value, --$start, $chars);
  325. }
  326. } // function MID()
  327. /**
  328. * RIGHT
  329. *
  330. * @param string $value Value
  331. * @param int $chars Number of characters
  332. * @return string
  333. */
  334. public static function RIGHT($value = '', $chars = 1) {
  335. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  336. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  337. if ($chars < 0) {
  338. return PHPExcel_Calculation_Functions::VALUE();
  339. }
  340. if (is_bool($value)) {
  341. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  342. }
  343. if ((function_exists('mb_substr')) && (function_exists('mb_strlen'))) {
  344. return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
  345. } else {
  346. return substr($value, strlen($value) - $chars);
  347. }
  348. } // function RIGHT()
  349. /**
  350. * STRINGLENGTH
  351. *
  352. * @param string $value Value
  353. * @return string
  354. */
  355. public static function STRINGLENGTH($value = '') {
  356. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  357. if (is_bool($value)) {
  358. $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  359. }
  360. if (function_exists('mb_strlen')) {
  361. return mb_strlen($value, 'UTF-8');
  362. } else {
  363. return strlen($value);
  364. }
  365. } // function STRINGLENGTH()
  366. /**
  367. * LOWERCASE
  368. *
  369. * Converts a string value to upper case.
  370. *
  371. * @param string $mixedCaseString
  372. * @return string
  373. */
  374. public static function LOWERCASE($mixedCaseString) {
  375. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  376. if (is_bool($mixedCaseString)) {
  377. $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  378. }
  379. return PHPExcel_Shared_String::StrToLower($mixedCaseString);
  380. } // function LOWERCASE()
  381. /**
  382. * UPPERCASE
  383. *
  384. * Converts a string value to upper case.
  385. *
  386. * @param string $mixedCaseString
  387. * @return string
  388. */
  389. public static function UPPERCASE($mixedCaseString) {
  390. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  391. if (is_bool($mixedCaseString)) {
  392. $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  393. }
  394. return PHPExcel_Shared_String::StrToUpper($mixedCaseString);
  395. } // function UPPERCASE()
  396. /**
  397. * PROPERCASE
  398. *
  399. * Converts a string value to upper case.
  400. *
  401. * @param string $mixedCaseString
  402. * @return string
  403. */
  404. public static function PROPERCASE($mixedCaseString) {
  405. $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString);
  406. if (is_bool($mixedCaseString)) {
  407. $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE();
  408. }
  409. return PHPExcel_Shared_String::StrToTitle($mixedCaseString);
  410. } // function PROPERCASE()
  411. /**
  412. * REPLACE
  413. *
  414. * @param string $oldText String to modify
  415. * @param int $start Start character
  416. * @param int $chars Number of characters
  417. * @param string $newText String to replace in defined position
  418. * @return string
  419. */
  420. public static function REPLACE($oldText = '', $start = 1, $chars = null, $newText) {
  421. $oldText = PHPExcel_Calculation_Functions::flattenSingleValue($oldText);
  422. $start = PHPExcel_Calculation_Functions::flattenSingleValue($start);
  423. $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars);
  424. $newText = PHPExcel_Calculation_Functions::flattenSingleValue($newText);
  425. $left = self::LEFT($oldText,$start-1);
  426. $right = self::RIGHT($oldText,self::STRINGLENGTH($oldText)-($start+$chars)+1);
  427. return $left.$newText.$right;
  428. } // function REPLACE()
  429. /**
  430. * SUBSTITUTE
  431. *
  432. * @param string $text Value
  433. * @param string $fromText From Value
  434. * @param string $toText To Value
  435. * @param integer $instance Instance Number
  436. * @return string
  437. */
  438. public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) {
  439. $text = PHPExcel_Calculation_Functions::flattenSingleValue($text);
  440. $fromText = PHPExcel_Calculation_Functions::flattenSingleValue($fromText);
  441. $toText = PHPExcel_Calculation_Functions::flattenSingleValue($toText);
  442. $instance = floor(PHPExcel_Calculation_Functions::flattenSingleValue($instance));
  443. if ($instance == 0) {
  444. if(function_exists('mb_str_replace')) {
  445. return mb_str_replace($fromText,$toText,$text);
  446. } else {
  447. return str_replace($fromText,$toText,$text);
  448. }
  449. } else {
  450. $pos = -1;
  451. while($instance > 0) {
  452. if (function_exists('mb_strpos')) {
  453. $pos = mb_strpos($text, $fromText, $pos+1, 'UTF-8');
  454. } else {
  455. $pos = strpos($text, $fromText, $pos+1);
  456. }
  457. if ($pos === false) {
  458. break;
  459. }
  460. --$instance;
  461. }
  462. if ($pos !== false) {
  463. if (function_exists('mb_strlen')) {
  464. return self::REPLACE($text,++$pos,mb_strlen($fromText, 'UTF-8'),$toText);
  465. } else {
  466. return self::REPLACE($text,++$pos,strlen($fromText),$toText);
  467. }
  468. }
  469. }
  470. return $text;
  471. } // function SUBSTITUTE()
  472. /**
  473. * RETURNSTRING
  474. *
  475. * @param mixed $testValue Value to check
  476. * @return boolean
  477. */
  478. public static function RETURNSTRING($testValue = '') {
  479. $testValue = PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
  480. if (is_string($testValue)) {
  481. return $testValue;
  482. }
  483. return Null;
  484. } // function RETURNSTRING()
  485. /**
  486. * TEXTFORMAT
  487. *
  488. * @param mixed $value Value to check
  489. * @param string $format Format mask to use
  490. * @return boolean
  491. */
  492. public static function TEXTFORMAT($value,$format) {
  493. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  494. $format = PHPExcel_Calculation_Functions::flattenSingleValue($format);
  495. if ((is_string($value)) && (!is_numeric($value)) && PHPExcel_Shared_Date::isDateTimeFormatCode($format)) {
  496. $value = PHPExcel_Calculation_DateTime::DATEVALUE($value);
  497. }
  498. return (string) PHPExcel_Style_NumberFormat::toFormattedString($value,$format);
  499. } // function TEXTFORMAT()
  500. } // class PHPExcel_Calculation_TextData