Multibyte.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. /**
  3. * Multibyte handling methods.
  4. *
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.I18n
  17. * @since CakePHP(tm) v 1.2.0.6833
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Multibyte handling methods.
  22. *
  23. * @package Cake.I18n
  24. */
  25. class Multibyte {
  26. /**
  27. * Holds the case folding values
  28. *
  29. * @var array
  30. */
  31. protected static $_caseFold = array();
  32. /**
  33. * Holds an array of Unicode code point ranges
  34. *
  35. * @var array
  36. */
  37. protected static $_codeRange = array();
  38. /**
  39. * Holds the current code point range
  40. *
  41. * @var string
  42. */
  43. protected static $_table = null;
  44. /**
  45. * Converts a multibyte character string
  46. * to the decimal value of the character
  47. *
  48. * @param string $string
  49. * @return array
  50. */
  51. public static function utf8($string) {
  52. $map = array();
  53. $values = array();
  54. $find = 1;
  55. $length = strlen($string);
  56. for ($i = 0; $i < $length; $i++) {
  57. $value = ord($string[$i]);
  58. if ($value < 128) {
  59. $map[] = $value;
  60. } else {
  61. if (empty($values)) {
  62. $find = ($value < 224) ? 2 : 3;
  63. }
  64. $values[] = $value;
  65. if (count($values) === $find) {
  66. if ($find == 3) {
  67. $map[] = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
  68. } else {
  69. $map[] = (($values[0] % 32) * 64) + ($values[1] % 64);
  70. }
  71. $values = array();
  72. $find = 1;
  73. }
  74. }
  75. }
  76. return $map;
  77. }
  78. /**
  79. * Converts the decimal value of a multibyte character string
  80. * to a string
  81. *
  82. * @param array $array
  83. * @return string
  84. */
  85. public static function ascii($array) {
  86. $ascii = '';
  87. foreach ($array as $utf8) {
  88. if ($utf8 < 128) {
  89. $ascii .= chr($utf8);
  90. } elseif ($utf8 < 2048) {
  91. $ascii .= chr(192 + (($utf8 - ($utf8 % 64)) / 64));
  92. $ascii .= chr(128 + ($utf8 % 64));
  93. } else {
  94. $ascii .= chr(224 + (($utf8 - ($utf8 % 4096)) / 4096));
  95. $ascii .= chr(128 + ((($utf8 % 4096) - ($utf8 % 64)) / 64));
  96. $ascii .= chr(128 + ($utf8 % 64));
  97. }
  98. }
  99. return $ascii;
  100. }
  101. /**
  102. * Find position of first occurrence of a case-insensitive string.
  103. *
  104. * @param string $haystack The string from which to get the position of the first occurrence of $needle.
  105. * @param string $needle The string to find in $haystack.
  106. * @param integer $offset The position in $haystack to start searching.
  107. * @return integer|boolean The numeric position of the first occurrence of $needle in the $haystack string,
  108. * or false if $needle is not found.
  109. */
  110. public static function stripos($haystack, $needle, $offset = 0) {
  111. if (Multibyte::checkMultibyte($haystack)) {
  112. $haystack = Multibyte::strtoupper($haystack);
  113. $needle = Multibyte::strtoupper($needle);
  114. return Multibyte::strpos($haystack, $needle, $offset);
  115. }
  116. return stripos($haystack, $needle, $offset);
  117. }
  118. /**
  119. * Finds first occurrence of a string within another, case insensitive.
  120. *
  121. * @param string $haystack The string from which to get the first occurrence of $needle.
  122. * @param string $needle The string to find in $haystack.
  123. * @param boolean $part Determines which portion of $haystack this function returns.
  124. * If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
  125. * If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
  126. * Default value is false.
  127. * @return integer|boolean The portion of $haystack, or false if $needle is not found.
  128. */
  129. public static function stristr($haystack, $needle, $part = false) {
  130. $php = (PHP_VERSION < 5.3);
  131. if (($php && $part) || Multibyte::checkMultibyte($haystack)) {
  132. $check = Multibyte::strtoupper($haystack);
  133. $check = Multibyte::utf8($check);
  134. $found = false;
  135. $haystack = Multibyte::utf8($haystack);
  136. $haystackCount = count($haystack);
  137. $needle = Multibyte::strtoupper($needle);
  138. $needle = Multibyte::utf8($needle);
  139. $needleCount = count($needle);
  140. $parts = array();
  141. $position = 0;
  142. while (($found === false) && ($position < $haystackCount)) {
  143. if (isset($needle[0]) && $needle[0] === $check[$position]) {
  144. for ($i = 1; $i < $needleCount; $i++) {
  145. if ($needle[$i] !== $check[$position + $i]) {
  146. break;
  147. }
  148. }
  149. if ($i === $needleCount) {
  150. $found = true;
  151. }
  152. }
  153. if (!$found) {
  154. $parts[] = $haystack[$position];
  155. unset($haystack[$position]);
  156. }
  157. $position++;
  158. }
  159. if ($found && $part && !empty($parts)) {
  160. return Multibyte::ascii($parts);
  161. } elseif ($found && !empty($haystack)) {
  162. return Multibyte::ascii($haystack);
  163. }
  164. return false;
  165. }
  166. if (!$php) {
  167. return stristr($haystack, $needle, $part);
  168. }
  169. return stristr($haystack, $needle);
  170. }
  171. /**
  172. * Get string length.
  173. *
  174. * @param string $string The string being checked for length.
  175. * @return integer The number of characters in string $string
  176. */
  177. public static function strlen($string) {
  178. if (Multibyte::checkMultibyte($string)) {
  179. $string = Multibyte::utf8($string);
  180. return count($string);
  181. }
  182. return strlen($string);
  183. }
  184. /**
  185. * Find position of first occurrence of a string.
  186. *
  187. * @param string $haystack The string being checked.
  188. * @param string $needle The position counted from the beginning of haystack.
  189. * @param integer $offset The search offset. If it is not specified, 0 is used.
  190. * @return integer|boolean The numeric position of the first occurrence of $needle in the $haystack string.
  191. * If $needle is not found, it returns false.
  192. */
  193. public static function strpos($haystack, $needle, $offset = 0) {
  194. if (Multibyte::checkMultibyte($haystack)) {
  195. $found = false;
  196. $haystack = Multibyte::utf8($haystack);
  197. $haystackCount = count($haystack);
  198. $needle = Multibyte::utf8($needle);
  199. $needleCount = count($needle);
  200. $position = $offset;
  201. while (($found === false) && ($position < $haystackCount)) {
  202. if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
  203. for ($i = 1; $i < $needleCount; $i++) {
  204. if ($needle[$i] !== $haystack[$position + $i]) {
  205. break;
  206. }
  207. }
  208. if ($i === $needleCount) {
  209. $found = true;
  210. $position--;
  211. }
  212. }
  213. $position++;
  214. }
  215. if ($found) {
  216. return $position;
  217. }
  218. return false;
  219. }
  220. return strpos($haystack, $needle, $offset);
  221. }
  222. /**
  223. * Finds the last occurrence of a character in a string within another.
  224. *
  225. * @param string $haystack The string from which to get the last occurrence of $needle.
  226. * @param string $needle The string to find in $haystack.
  227. * @param boolean $part Determines which portion of $haystack this function returns.
  228. * If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
  229. * If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
  230. * Default value is false.
  231. * @return string|boolean The portion of $haystack. or false if $needle is not found.
  232. */
  233. public static function strrchr($haystack, $needle, $part = false) {
  234. $check = Multibyte::utf8($haystack);
  235. $found = false;
  236. $haystack = Multibyte::utf8($haystack);
  237. $haystackCount = count($haystack);
  238. $matches = array_count_values($check);
  239. $needle = Multibyte::utf8($needle);
  240. $needleCount = count($needle);
  241. $parts = array();
  242. $position = 0;
  243. while (($found === false) && ($position < $haystackCount)) {
  244. if (isset($needle[0]) && $needle[0] === $check[$position]) {
  245. for ($i = 1; $i < $needleCount; $i++) {
  246. if ($needle[$i] !== $check[$position + $i]) {
  247. if ($needle[$i] === $check[($position + $i) - 1]) {
  248. $found = true;
  249. }
  250. unset($parts[$position - 1]);
  251. $haystack = array_merge(array($haystack[$position]), $haystack);
  252. break;
  253. }
  254. }
  255. if (isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
  256. $matches[$needle[0]] = $matches[$needle[0]] - 1;
  257. } elseif ($i === $needleCount) {
  258. $found = true;
  259. }
  260. }
  261. if (!$found && isset($haystack[$position])) {
  262. $parts[] = $haystack[$position];
  263. unset($haystack[$position]);
  264. }
  265. $position++;
  266. }
  267. if ($found && $part && !empty($parts)) {
  268. return Multibyte::ascii($parts);
  269. } elseif ($found && !empty($haystack)) {
  270. return Multibyte::ascii($haystack);
  271. }
  272. return false;
  273. }
  274. /**
  275. * Finds the last occurrence of a character in a string within another, case insensitive.
  276. *
  277. * @param string $haystack The string from which to get the last occurrence of $needle.
  278. * @param string $needle The string to find in $haystack.
  279. * @param boolean $part Determines which portion of $haystack this function returns.
  280. * If set to true, it returns all of $haystack from the beginning to the last occurrence of $needle.
  281. * If set to false, it returns all of $haystack from the last occurrence of $needle to the end,
  282. * Default value is false.
  283. * @return string|boolean The portion of $haystack. or false if $needle is not found.
  284. */
  285. public static function strrichr($haystack, $needle, $part = false) {
  286. $check = Multibyte::strtoupper($haystack);
  287. $check = Multibyte::utf8($check);
  288. $found = false;
  289. $haystack = Multibyte::utf8($haystack);
  290. $haystackCount = count($haystack);
  291. $matches = array_count_values($check);
  292. $needle = Multibyte::strtoupper($needle);
  293. $needle = Multibyte::utf8($needle);
  294. $needleCount = count($needle);
  295. $parts = array();
  296. $position = 0;
  297. while (($found === false) && ($position < $haystackCount)) {
  298. if (isset($needle[0]) && $needle[0] === $check[$position]) {
  299. for ($i = 1; $i < $needleCount; $i++) {
  300. if ($needle[$i] !== $check[$position + $i]) {
  301. if ($needle[$i] === $check[($position + $i) - 1]) {
  302. $found = true;
  303. }
  304. unset($parts[$position - 1]);
  305. $haystack = array_merge(array($haystack[$position]), $haystack);
  306. break;
  307. }
  308. }
  309. if (isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
  310. $matches[$needle[0]] = $matches[$needle[0]] - 1;
  311. } elseif ($i === $needleCount) {
  312. $found = true;
  313. }
  314. }
  315. if (!$found && isset($haystack[$position])) {
  316. $parts[] = $haystack[$position];
  317. unset($haystack[$position]);
  318. }
  319. $position++;
  320. }
  321. if ($found && $part && !empty($parts)) {
  322. return Multibyte::ascii($parts);
  323. } elseif ($found && !empty($haystack)) {
  324. return Multibyte::ascii($haystack);
  325. }
  326. return false;
  327. }
  328. /**
  329. * Finds position of last occurrence of a string within another, case insensitive
  330. *
  331. * @param string $haystack The string from which to get the position of the last occurrence of $needle.
  332. * @param string $needle The string to find in $haystack.
  333. * @param integer $offset The position in $haystack to start searching.
  334. * @return integer|boolean The numeric position of the last occurrence of $needle in the $haystack string,
  335. * or false if $needle is not found.
  336. */
  337. public static function strripos($haystack, $needle, $offset = 0) {
  338. if (Multibyte::checkMultibyte($haystack)) {
  339. $found = false;
  340. $haystack = Multibyte::strtoupper($haystack);
  341. $haystack = Multibyte::utf8($haystack);
  342. $haystackCount = count($haystack);
  343. $matches = array_count_values($haystack);
  344. $needle = Multibyte::strtoupper($needle);
  345. $needle = Multibyte::utf8($needle);
  346. $needleCount = count($needle);
  347. $position = $offset;
  348. while (($found === false) && ($position < $haystackCount)) {
  349. if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
  350. for ($i = 1; $i < $needleCount; $i++) {
  351. if ($needle[$i] !== $haystack[$position + $i]) {
  352. if ($needle[$i] === $haystack[($position + $i) - 1]) {
  353. $position--;
  354. $found = true;
  355. continue;
  356. }
  357. }
  358. }
  359. if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
  360. $matches[$needle[0]] = $matches[$needle[0]] - 1;
  361. } elseif ($i === $needleCount) {
  362. $found = true;
  363. $position--;
  364. }
  365. }
  366. $position++;
  367. }
  368. return ($found) ? $position : false;
  369. }
  370. return strripos($haystack, $needle, $offset);
  371. }
  372. /**
  373. * Find position of last occurrence of a string in a string.
  374. *
  375. * @param string $haystack The string being checked, for the last occurrence of $needle.
  376. * @param string $needle The string to find in $haystack.
  377. * @param integer $offset May be specified to begin searching an arbitrary number of characters into the string.
  378. * Negative values will stop searching at an arbitrary point prior to the end of the string.
  379. * @return integer|boolean The numeric position of the last occurrence of $needle in the $haystack string.
  380. * If $needle is not found, it returns false.
  381. */
  382. public static function strrpos($haystack, $needle, $offset = 0) {
  383. if (Multibyte::checkMultibyte($haystack)) {
  384. $found = false;
  385. $haystack = Multibyte::utf8($haystack);
  386. $haystackCount = count($haystack);
  387. $matches = array_count_values($haystack);
  388. $needle = Multibyte::utf8($needle);
  389. $needleCount = count($needle);
  390. $position = $offset;
  391. while (($found === false) && ($position < $haystackCount)) {
  392. if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
  393. for ($i = 1; $i < $needleCount; $i++) {
  394. if ($needle[$i] !== $haystack[$position + $i]) {
  395. if ($needle[$i] === $haystack[($position + $i) - 1]) {
  396. $position--;
  397. $found = true;
  398. continue;
  399. }
  400. }
  401. }
  402. if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
  403. $matches[$needle[0]] = $matches[$needle[0]] - 1;
  404. } elseif ($i === $needleCount) {
  405. $found = true;
  406. $position--;
  407. }
  408. }
  409. $position++;
  410. }
  411. return ($found) ? $position : false;
  412. }
  413. return strrpos($haystack, $needle, $offset);
  414. }
  415. /**
  416. * Finds first occurrence of a string within another
  417. *
  418. * @param string $haystack The string from which to get the first occurrence of $needle.
  419. * @param string $needle The string to find in $haystack
  420. * @param boolean $part Determines which portion of $haystack this function returns.
  421. * If set to true, it returns all of $haystack from the beginning to the first occurrence of $needle.
  422. * If set to false, it returns all of $haystack from the first occurrence of $needle to the end,
  423. * Default value is FALSE.
  424. * @return string|boolean The portion of $haystack, or true if $needle is not found.
  425. */
  426. public static function strstr($haystack, $needle, $part = false) {
  427. $php = (PHP_VERSION < 5.3);
  428. if (($php && $part) || Multibyte::checkMultibyte($haystack)) {
  429. $check = Multibyte::utf8($haystack);
  430. $found = false;
  431. $haystack = Multibyte::utf8($haystack);
  432. $haystackCount = count($haystack);
  433. $needle = Multibyte::utf8($needle);
  434. $needleCount = count($needle);
  435. $parts = array();
  436. $position = 0;
  437. while (($found === false) && ($position < $haystackCount)) {
  438. if (isset($needle[0]) && $needle[0] === $check[$position]) {
  439. for ($i = 1; $i < $needleCount; $i++) {
  440. if ($needle[$i] !== $check[$position + $i]) {
  441. break;
  442. }
  443. }
  444. if ($i === $needleCount) {
  445. $found = true;
  446. }
  447. }
  448. if (!$found) {
  449. $parts[] = $haystack[$position];
  450. unset($haystack[$position]);
  451. }
  452. $position++;
  453. }
  454. if ($found && $part && !empty($parts)) {
  455. return Multibyte::ascii($parts);
  456. } elseif ($found && !empty($haystack)) {
  457. return Multibyte::ascii($haystack);
  458. }
  459. return false;
  460. }
  461. if (!$php) {
  462. return strstr($haystack, $needle, $part);
  463. }
  464. return strstr($haystack, $needle);
  465. }
  466. /**
  467. * Make a string lowercase
  468. *
  469. * @param string $string The string being lowercased.
  470. * @return string with all alphabetic characters converted to lowercase.
  471. */
  472. public static function strtolower($string) {
  473. $utf8Map = Multibyte::utf8($string);
  474. $length = count($utf8Map);
  475. $lowerCase = array();
  476. for ($i = 0; $i < $length; $i++) {
  477. $char = $utf8Map[$i];
  478. if ($char < 128) {
  479. $str = strtolower(chr($char));
  480. $strlen = strlen($str);
  481. for ($ii = 0; $ii < $strlen; $ii++) {
  482. $lower = ord(substr($str, $ii, 1));
  483. }
  484. $lowerCase[] = $lower;
  485. $matched = true;
  486. } else {
  487. $matched = false;
  488. $keys = self::_find($char, 'upper');
  489. if (!empty($keys)) {
  490. foreach ($keys as $key => $value) {
  491. if ($keys[$key]['upper'] == $char && count($keys[$key]['lower'][0]) === 1) {
  492. $lowerCase[] = $keys[$key]['lower'][0];
  493. $matched = true;
  494. break 1;
  495. }
  496. }
  497. }
  498. }
  499. if ($matched === false) {
  500. $lowerCase[] = $char;
  501. }
  502. }
  503. return Multibyte::ascii($lowerCase);
  504. }
  505. /**
  506. * Make a string uppercase
  507. *
  508. * @param string $string The string being uppercased.
  509. * @return string with all alphabetic characters converted to uppercase.
  510. */
  511. public static function strtoupper($string) {
  512. $utf8Map = Multibyte::utf8($string);
  513. $length = count($utf8Map);
  514. $replaced = array();
  515. $upperCase = array();
  516. for ($i = 0; $i < $length; $i++) {
  517. $char = $utf8Map[$i];
  518. if ($char < 128) {
  519. $str = strtoupper(chr($char));
  520. $strlen = strlen($str);
  521. for ($ii = 0; $ii < $strlen; $ii++) {
  522. $upper = ord(substr($str, $ii, 1));
  523. }
  524. $upperCase[] = $upper;
  525. $matched = true;
  526. } else {
  527. $matched = false;
  528. $keys = self::_find($char);
  529. $keyCount = count($keys);
  530. if (!empty($keys)) {
  531. foreach ($keys as $key => $value) {
  532. $matched = false;
  533. $replace = 0;
  534. if ($length > 1 && count($keys[$key]['lower']) > 1) {
  535. $j = 0;
  536. for ($ii = 0, $count = count($keys[$key]['lower']); $ii < $count; $ii++) {
  537. $nextChar = $utf8Map[$i + $ii];
  538. if (isset($nextChar) && ($nextChar == $keys[$key]['lower'][$j + $ii])) {
  539. $replace++;
  540. }
  541. }
  542. if ($replace == $count) {
  543. $upperCase[] = $keys[$key]['upper'];
  544. $replaced = array_merge($replaced, array_values($keys[$key]['lower']));
  545. $matched = true;
  546. break 1;
  547. }
  548. } elseif ($length > 1 && $keyCount > 1) {
  549. $j = 0;
  550. for ($ii = 1; $ii < $keyCount; $ii++) {
  551. $nextChar = $utf8Map[$i + $ii - 1];
  552. if (in_array($nextChar, $keys[$ii]['lower'])) {
  553. for ($jj = 0, $count = count($keys[$ii]['lower']); $jj < $count; $jj++) {
  554. $nextChar = $utf8Map[$i + $jj];
  555. if (isset($nextChar) && ($nextChar == $keys[$ii]['lower'][$j + $jj])) {
  556. $replace++;
  557. }
  558. }
  559. if ($replace == $count) {
  560. $upperCase[] = $keys[$ii]['upper'];
  561. $replaced = array_merge($replaced, array_values($keys[$ii]['lower']));
  562. $matched = true;
  563. break 2;
  564. }
  565. }
  566. }
  567. }
  568. if ($keys[$key]['lower'][0] == $char) {
  569. $upperCase[] = $keys[$key]['upper'];
  570. $matched = true;
  571. break 1;
  572. }
  573. }
  574. }
  575. }
  576. if ($matched === false && !in_array($char, $replaced, true)) {
  577. $upperCase[] = $char;
  578. }
  579. }
  580. return Multibyte::ascii($upperCase);
  581. }
  582. /**
  583. * Count the number of substring occurrences
  584. *
  585. * @param string $haystack The string being checked.
  586. * @param string $needle The string being found.
  587. * @return integer The number of times the $needle substring occurs in the $haystack string.
  588. */
  589. public static function substrCount($haystack, $needle) {
  590. $count = 0;
  591. $haystack = Multibyte::utf8($haystack);
  592. $haystackCount = count($haystack);
  593. $matches = array_count_values($haystack);
  594. $needle = Multibyte::utf8($needle);
  595. $needleCount = count($needle);
  596. if ($needleCount === 1 && isset($matches[$needle[0]])) {
  597. return $matches[$needle[0]];
  598. }
  599. for ($i = 0; $i < $haystackCount; $i++) {
  600. if (isset($needle[0]) && $needle[0] === $haystack[$i]) {
  601. for ($ii = 1; $ii < $needleCount; $ii++) {
  602. if ($needle[$ii] === $haystack[$i + 1]) {
  603. if ((isset($needle[$ii + 1]) && $haystack[$i + 2]) && $needle[$ii + 1] !== $haystack[$i + 2]) {
  604. $count--;
  605. } else {
  606. $count++;
  607. }
  608. }
  609. }
  610. }
  611. }
  612. return $count;
  613. }
  614. /**
  615. * Get part of string
  616. *
  617. * @param string $string The string being checked.
  618. * @param integer $start The first position used in $string.
  619. * @param integer $length The maximum length of the returned string.
  620. * @return string The portion of $string specified by the $string and $length parameters.
  621. */
  622. public static function substr($string, $start, $length = null) {
  623. if ($start === 0 && $length === null) {
  624. return $string;
  625. }
  626. $string = Multibyte::utf8($string);
  627. for ($i = 1; $i <= $start; $i++) {
  628. unset($string[$i - 1]);
  629. }
  630. if ($length === null || count($string) < $length) {
  631. return Multibyte::ascii($string);
  632. }
  633. $string = array_values($string);
  634. $value = array();
  635. for ($i = 0; $i < $length; $i++) {
  636. $value[] = $string[$i];
  637. }
  638. return Multibyte::ascii($value);
  639. }
  640. /**
  641. * Prepare a string for mail transport, using the provided encoding
  642. *
  643. * @param string $string value to encode
  644. * @param string $charset charset to use for encoding. defaults to UTF-8
  645. * @param string $newline
  646. * @return string
  647. */
  648. public static function mimeEncode($string, $charset = null, $newline = "\r\n") {
  649. if (!Multibyte::checkMultibyte($string) && strlen($string) < 75) {
  650. return $string;
  651. }
  652. if (empty($charset)) {
  653. $charset = Configure::read('App.encoding');
  654. }
  655. $charset = strtoupper($charset);
  656. $start = '=?' . $charset . '?B?';
  657. $end = '?=';
  658. $spacer = $end . $newline . ' ' . $start;
  659. $length = 75 - strlen($start) - strlen($end);
  660. $length = $length - ($length % 4);
  661. if ($charset == 'UTF-8') {
  662. $parts = array();
  663. $maxchars = floor(($length * 3) / 4);
  664. $stringLength = strlen($string);
  665. while ($stringLength > $maxchars) {
  666. $i = (int)$maxchars;
  667. $test = ord($string[$i]);
  668. while ($test >= 128 && $test <= 191) {
  669. $i--;
  670. $test = ord($string[$i]);
  671. }
  672. $parts[] = base64_encode(substr($string, 0, $i));
  673. $string = substr($string, $i);
  674. $stringLength = strlen($string);
  675. }
  676. $parts[] = base64_encode($string);
  677. $string = implode($spacer, $parts);
  678. } else {
  679. $string = chunk_split(base64_encode($string), $length, $spacer);
  680. $string = preg_replace('/' . preg_quote($spacer) . '$/', '', $string);
  681. }
  682. return $start . $string . $end;
  683. }
  684. /**
  685. * Return the Code points range for Unicode characters
  686. *
  687. * @param integer $decimal
  688. * @return string
  689. */
  690. protected static function _codepoint($decimal) {
  691. if ($decimal > 128 && $decimal < 256) {
  692. $return = '0080_00ff'; // Latin-1 Supplement
  693. } elseif ($decimal < 384) {
  694. $return = '0100_017f'; // Latin Extended-A
  695. } elseif ($decimal < 592) {
  696. $return = '0180_024F'; // Latin Extended-B
  697. } elseif ($decimal < 688) {
  698. $return = '0250_02af'; // IPA Extensions
  699. } elseif ($decimal >= 880 && $decimal < 1024) {
  700. $return = '0370_03ff'; // Greek and Coptic
  701. } elseif ($decimal < 1280) {
  702. $return = '0400_04ff'; // Cyrillic
  703. } elseif ($decimal < 1328) {
  704. $return = '0500_052f'; // Cyrillic Supplement
  705. } elseif ($decimal < 1424) {
  706. $return = '0530_058f'; // Armenian
  707. } elseif ($decimal >= 7680 && $decimal < 7936) {
  708. $return = '1e00_1eff'; // Latin Extended Additional
  709. } elseif ($decimal < 8192) {
  710. $return = '1f00_1fff'; // Greek Extended
  711. } elseif ($decimal >= 8448 && $decimal < 8528) {
  712. $return = '2100_214f'; // Letterlike Symbols
  713. } elseif ($decimal < 8592) {
  714. $return = '2150_218f'; // Number Forms
  715. } elseif ($decimal >= 9312 && $decimal < 9472) {
  716. $return = '2460_24ff'; // Enclosed Alphanumerics
  717. } elseif ($decimal >= 11264 && $decimal < 11360) {
  718. $return = '2c00_2c5f'; // Glagolitic
  719. } elseif ($decimal < 11392) {
  720. $return = '2c60_2c7f'; // Latin Extended-C
  721. } elseif ($decimal < 11520) {
  722. $return = '2c80_2cff'; // Coptic
  723. } elseif ($decimal >= 65280 && $decimal < 65520) {
  724. $return = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
  725. } else {
  726. $return = false;
  727. }
  728. self::$_codeRange[$decimal] = $return;
  729. return $return;
  730. }
  731. /**
  732. * Find the related code folding values for $char
  733. *
  734. * @param integer $char decimal value of character
  735. * @param string $type
  736. * @return array
  737. */
  738. protected static function _find($char, $type = 'lower') {
  739. $found = array();
  740. if (!isset(self::$_codeRange[$char])) {
  741. $range = self::_codepoint($char);
  742. if ($range === false) {
  743. return null;
  744. }
  745. if (!Configure::configured('_cake_core_')) {
  746. App::uses('PhpReader', 'Configure');
  747. Configure::config('_cake_core_', new PhpReader(CAKE . 'Config' . DS));
  748. }
  749. Configure::load('unicode' . DS . 'casefolding' . DS . $range, '_cake_core_');
  750. self::$_caseFold[$range] = Configure::read($range);
  751. Configure::delete($range);
  752. }
  753. if (!self::$_codeRange[$char]) {
  754. return null;
  755. }
  756. self::$_table = self::$_codeRange[$char];
  757. $count = count(self::$_caseFold[self::$_table]);
  758. for ($i = 0; $i < $count; $i++) {
  759. if ($type === 'lower' && self::$_caseFold[self::$_table][$i][$type][0] === $char) {
  760. $found[] = self::$_caseFold[self::$_table][$i];
  761. } elseif ($type === 'upper' && self::$_caseFold[self::$_table][$i][$type] === $char) {
  762. $found[] = self::$_caseFold[self::$_table][$i];
  763. }
  764. }
  765. return $found;
  766. }
  767. /**
  768. * Check the $string for multibyte characters
  769. * @param string $string value to test
  770. * @return boolean
  771. */
  772. public static function checkMultibyte($string) {
  773. $length = strlen($string);
  774. for ($i = 0; $i < $length; $i++) {
  775. $value = ord(($string[$i]));
  776. if ($value > 128) {
  777. return true;
  778. }
  779. }
  780. return false;
  781. }
  782. }