Ei kuvausta

Mbstring.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Mbstring;
  11. /**
  12. * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
  13. *
  14. * Implemented:
  15. * - mb_chr - Returns a specific character from its Unicode code point
  16. * - mb_convert_encoding - Convert character encoding
  17. * - mb_convert_variables - Convert character code in variable(s)
  18. * - mb_decode_mimeheader - Decode string in MIME header field
  19. * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
  20. * - mb_convert_case - Perform case folding on a string
  21. * - mb_get_info - Get internal settings of mbstring
  22. * - mb_http_input - Detect HTTP input character encoding
  23. * - mb_http_output - Set/Get HTTP output character encoding
  24. * - mb_internal_encoding - Set/Get internal character encoding
  25. * - mb_list_encodings - Returns an array of all supported encodings
  26. * - mb_ord - Returns the Unicode code point of a character
  27. * - mb_output_handler - Callback function converts character encoding in output buffer
  28. * - mb_scrub - Replaces ill-formed byte sequences with substitute characters
  29. * - mb_strlen - Get string length
  30. * - mb_strpos - Find position of first occurrence of string in a string
  31. * - mb_strrpos - Find position of last occurrence of a string in a string
  32. * - mb_strtolower - Make a string lowercase
  33. * - mb_strtoupper - Make a string uppercase
  34. * - mb_substitute_character - Set/Get substitution character
  35. * - mb_substr - Get part of string
  36. * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
  37. * - mb_stristr - Finds first occurrence of a string within another, case insensitive
  38. * - mb_strrchr - Finds the last occurrence of a character in a string within another
  39. * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
  40. * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
  41. * - mb_strstr - Finds first occurrence of a string within anothers
  42. * - mb_strwidth - Return width of string
  43. * - mb_substr_count - Count the number of substring occurrences
  44. *
  45. * Not implemented:
  46. * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
  47. * - mb_decode_numericentity - Decode HTML numeric string reference to character
  48. * - mb_encode_numericentity - Encode character to HTML numeric string reference
  49. * - mb_ereg_* - Regular expression with multibyte support
  50. * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
  51. * - mb_preferred_mime_name - Get MIME charset string
  52. * - mb_regex_encoding - Returns current encoding for multibyte regex as string
  53. * - mb_regex_set_options - Set/Get the default options for mbregex functions
  54. * - mb_send_mail - Send encoded mail
  55. * - mb_split - Split multibyte string using regular expression
  56. * - mb_strcut - Get part of string
  57. * - mb_strimwidth - Get truncated string with specified width
  58. *
  59. * @author Nicolas Grekas <p@tchwork.com>
  60. *
  61. * @internal
  62. */
  63. final class Mbstring
  64. {
  65. const MB_CASE_FOLD = PHP_INT_MAX;
  66. private static $encodingList = array('ASCII', 'UTF-8');
  67. private static $language = 'neutral';
  68. private static $internalEncoding = 'UTF-8';
  69. private static $caseFold = array(
  70. array('µ','ſ',"\xCD\x85",'ς',"\xCF\x90","\xCF\x91","\xCF\x95","\xCF\x96","\xCF\xB0","\xCF\xB1","\xCF\xB5","\xE1\xBA\x9B","\xE1\xBE\xBE"),
  71. array('μ','s','ι', 'σ','β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1",'ι'),
  72. );
  73. public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
  74. {
  75. if (is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
  76. $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
  77. } else {
  78. $fromEncoding = self::getEncoding($fromEncoding);
  79. }
  80. $toEncoding = self::getEncoding($toEncoding);
  81. if ('BASE64' === $fromEncoding) {
  82. $s = base64_decode($s);
  83. $fromEncoding = $toEncoding;
  84. }
  85. if ('BASE64' === $toEncoding) {
  86. return base64_encode($s);
  87. }
  88. if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
  89. if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
  90. $fromEncoding = 'Windows-1252';
  91. }
  92. if ('UTF-8' !== $fromEncoding) {
  93. $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
  94. }
  95. return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
  96. }
  97. if ('HTML-ENTITIES' === $fromEncoding) {
  98. $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
  99. $fromEncoding = 'UTF-8';
  100. }
  101. return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
  102. }
  103. public static function mb_convert_variables($toEncoding, $fromEncoding, &$a = null, &$b = null, &$c = null, &$d = null, &$e = null, &$f = null)
  104. {
  105. $vars = array(&$a, &$b, &$c, &$d, &$e, &$f);
  106. $ok = true;
  107. array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
  108. if (false === $v = Mbstring::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
  109. $ok = false;
  110. }
  111. });
  112. return $ok ? $fromEncoding : false;
  113. }
  114. public static function mb_decode_mimeheader($s)
  115. {
  116. return iconv_mime_decode($s, 2, self::$internalEncoding);
  117. }
  118. public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
  119. {
  120. trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
  121. }
  122. public static function mb_convert_case($s, $mode, $encoding = null)
  123. {
  124. if ('' === $s .= '') {
  125. return '';
  126. }
  127. $encoding = self::getEncoding($encoding);
  128. if ('UTF-8' === $encoding) {
  129. $encoding = null;
  130. } else {
  131. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  132. }
  133. if (MB_CASE_TITLE == $mode) {
  134. $s = preg_replace_callback('/\b\p{Ll}/u', array(__CLASS__, 'title_case_upper'), $s);
  135. $s = preg_replace_callback('/\B[\p{Lu}\p{Lt}]+/u', array(__CLASS__, 'title_case_lower'), $s);
  136. } else {
  137. if (MB_CASE_UPPER == $mode) {
  138. static $upper = null;
  139. if (null === $upper) {
  140. $upper = self::getData('upperCase');
  141. }
  142. $map = $upper;
  143. } else {
  144. if (self::MB_CASE_FOLD === $mode) {
  145. $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
  146. }
  147. static $lower = null;
  148. if (null === $lower) {
  149. $lower = self::getData('lowerCase');
  150. }
  151. $map = $lower;
  152. }
  153. static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
  154. $i = 0;
  155. $len = strlen($s);
  156. while ($i < $len) {
  157. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  158. $uchr = substr($s, $i, $ulen);
  159. $i += $ulen;
  160. if (isset($map[$uchr])) {
  161. $uchr = $map[$uchr];
  162. $nlen = strlen($uchr);
  163. if ($nlen == $ulen) {
  164. $nlen = $i;
  165. do {
  166. $s[--$nlen] = $uchr[--$ulen];
  167. } while ($ulen);
  168. } else {
  169. $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
  170. $len += $nlen - $ulen;
  171. $i += $nlen - $ulen;
  172. }
  173. }
  174. }
  175. }
  176. if (null === $encoding) {
  177. return $s;
  178. }
  179. return iconv('UTF-8', $encoding.'//IGNORE', $s);
  180. }
  181. public static function mb_internal_encoding($encoding = null)
  182. {
  183. if (null === $encoding) {
  184. return self::$internalEncoding;
  185. }
  186. $encoding = self::getEncoding($encoding);
  187. if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
  188. self::$internalEncoding = $encoding;
  189. return true;
  190. }
  191. return false;
  192. }
  193. public static function mb_language($lang = null)
  194. {
  195. if (null === $lang) {
  196. return self::$language;
  197. }
  198. switch ($lang = strtolower($lang)) {
  199. case 'uni':
  200. case 'neutral':
  201. self::$language = $lang;
  202. return true;
  203. }
  204. return false;
  205. }
  206. public static function mb_list_encodings()
  207. {
  208. return array('UTF-8');
  209. }
  210. public static function mb_encoding_aliases($encoding)
  211. {
  212. switch (strtoupper($encoding)) {
  213. case 'UTF8':
  214. case 'UTF-8':
  215. return array('utf8');
  216. }
  217. return false;
  218. }
  219. public static function mb_check_encoding($var = null, $encoding = null)
  220. {
  221. if (null === $encoding) {
  222. if (null === $var) {
  223. return false;
  224. }
  225. $encoding = self::$internalEncoding;
  226. }
  227. return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var);
  228. }
  229. public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
  230. {
  231. if (null === $encodingList) {
  232. $encodingList = self::$encodingList;
  233. } else {
  234. if (!is_array($encodingList)) {
  235. $encodingList = array_map('trim', explode(',', $encodingList));
  236. }
  237. $encodingList = array_map('strtoupper', $encodingList);
  238. }
  239. foreach ($encodingList as $enc) {
  240. switch ($enc) {
  241. case 'ASCII':
  242. if (!preg_match('/[\x80-\xFF]/', $str)) {
  243. return $enc;
  244. }
  245. break;
  246. case 'UTF8':
  247. case 'UTF-8':
  248. if (preg_match('//u', $str)) {
  249. return 'UTF-8';
  250. }
  251. break;
  252. default:
  253. if (0 === strncmp($enc, 'ISO-8859-', 9)) {
  254. return $enc;
  255. }
  256. }
  257. }
  258. return false;
  259. }
  260. public static function mb_detect_order($encodingList = null)
  261. {
  262. if (null === $encodingList) {
  263. return self::$encodingList;
  264. }
  265. if (!is_array($encodingList)) {
  266. $encodingList = array_map('trim', explode(',', $encodingList));
  267. }
  268. $encodingList = array_map('strtoupper', $encodingList);
  269. foreach ($encodingList as $enc) {
  270. switch ($enc) {
  271. default:
  272. if (strncmp($enc, 'ISO-8859-', 9)) {
  273. return false;
  274. }
  275. case 'ASCII':
  276. case 'UTF8':
  277. case 'UTF-8':
  278. }
  279. }
  280. self::$encodingList = $encodingList;
  281. return true;
  282. }
  283. public static function mb_strlen($s, $encoding = null)
  284. {
  285. switch ($encoding = self::getEncoding($encoding)) {
  286. case 'ASCII':
  287. case 'CP850':
  288. return strlen($s);
  289. }
  290. return @iconv_strlen($s, $encoding);
  291. }
  292. public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
  293. {
  294. $encoding = self::getEncoding($encoding);
  295. if ('' === $needle .= '') {
  296. trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
  297. return false;
  298. }
  299. return iconv_strpos($haystack, $needle, $offset, $encoding);
  300. }
  301. public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
  302. {
  303. $encoding = self::getEncoding($encoding);
  304. if ($offset != (int) $offset) {
  305. $offset = 0;
  306. } elseif ($offset = (int) $offset) {
  307. if ($offset < 0) {
  308. $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
  309. $offset = 0;
  310. } else {
  311. $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
  312. }
  313. }
  314. $pos = iconv_strrpos($haystack, $needle, $encoding);
  315. return false !== $pos ? $offset + $pos : false;
  316. }
  317. public static function mb_strtolower($s, $encoding = null)
  318. {
  319. return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
  320. }
  321. public static function mb_strtoupper($s, $encoding = null)
  322. {
  323. return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
  324. }
  325. public static function mb_substitute_character($c = null)
  326. {
  327. if (0 === strcasecmp($c, 'none')) {
  328. return true;
  329. }
  330. return null !== $c ? false : 'none';
  331. }
  332. public static function mb_substr($s, $start, $length = null, $encoding = null)
  333. {
  334. $encoding = self::getEncoding($encoding);
  335. if ($start < 0) {
  336. $start = iconv_strlen($s, $encoding) + $start;
  337. if ($start < 0) {
  338. $start = 0;
  339. }
  340. }
  341. if (null === $length) {
  342. $length = 2147483647;
  343. } elseif ($length < 0) {
  344. $length = iconv_strlen($s, $encoding) + $length - $start;
  345. if ($length < 0) {
  346. return '';
  347. }
  348. }
  349. return iconv_substr($s, $start, $length, $encoding).'';
  350. }
  351. public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
  352. {
  353. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  354. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  355. return self::mb_strpos($haystack, $needle, $offset, $encoding);
  356. }
  357. public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
  358. {
  359. $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
  360. return self::getSubpart($pos, $part, $haystack, $encoding);
  361. }
  362. public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
  363. {
  364. $encoding = self::getEncoding($encoding);
  365. $needle = self::mb_substr($needle, 0, 1, $encoding);
  366. $pos = iconv_strrpos($haystack, $needle, $encoding);
  367. return self::getSubpart($pos, $part, $haystack, $encoding);
  368. }
  369. public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
  370. {
  371. $needle = self::mb_substr($needle, 0, 1, $encoding);
  372. $pos = self::mb_strripos($haystack, $needle, $encoding);
  373. return self::getSubpart($pos, $part, $haystack, $encoding);
  374. }
  375. public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
  376. {
  377. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  378. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  379. return self::mb_strrpos($haystack, $needle, $offset, $encoding);
  380. }
  381. public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
  382. {
  383. $pos = strpos($haystack, $needle);
  384. if (false === $pos) {
  385. return false;
  386. }
  387. if ($part) {
  388. return substr($haystack, 0, $pos);
  389. }
  390. return substr($haystack, $pos);
  391. }
  392. public static function mb_get_info($type = 'all')
  393. {
  394. $info = array(
  395. 'internal_encoding' => self::$internalEncoding,
  396. 'http_output' => 'pass',
  397. 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
  398. 'func_overload' => 0,
  399. 'func_overload_list' => 'no overload',
  400. 'mail_charset' => 'UTF-8',
  401. 'mail_header_encoding' => 'BASE64',
  402. 'mail_body_encoding' => 'BASE64',
  403. 'illegal_chars' => 0,
  404. 'encoding_translation' => 'Off',
  405. 'language' => self::$language,
  406. 'detect_order' => self::$encodingList,
  407. 'substitute_character' => 'none',
  408. 'strict_detection' => 'Off',
  409. );
  410. if ('all' === $type) {
  411. return $info;
  412. }
  413. if (isset($info[$type])) {
  414. return $info[$type];
  415. }
  416. return false;
  417. }
  418. public static function mb_http_input($type = '')
  419. {
  420. return false;
  421. }
  422. public static function mb_http_output($encoding = null)
  423. {
  424. return null !== $encoding ? 'pass' === $encoding : 'pass';
  425. }
  426. public static function mb_strwidth($s, $encoding = null)
  427. {
  428. $encoding = self::getEncoding($encoding);
  429. if ('UTF-8' !== $encoding) {
  430. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  431. }
  432. $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide);
  433. return ($wide << 1) + iconv_strlen($s, 'UTF-8');
  434. }
  435. public static function mb_substr_count($haystack, $needle, $encoding = null)
  436. {
  437. return substr_count($haystack, $needle);
  438. }
  439. public static function mb_output_handler($contents, $status)
  440. {
  441. return $contents;
  442. }
  443. public static function mb_chr($code, $encoding = null)
  444. {
  445. if (0x80 > $code %= 0x200000) {
  446. $s = chr($code);
  447. } elseif (0x800 > $code) {
  448. $s = chr(0xC0 | $code >> 6).chr(0x80 | $code & 0x3F);
  449. } elseif (0x10000 > $code) {
  450. $s = chr(0xE0 | $code >> 12).chr(0x80 | $code >> 6 & 0x3F).chr(0x80 | $code & 0x3F);
  451. } else {
  452. $s = chr(0xF0 | $code >> 18).chr(0x80 | $code >> 12 & 0x3F).chr(0x80 | $code >> 6 & 0x3F).chr(0x80 | $code & 0x3F);
  453. }
  454. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  455. $s = mb_convert_encoding($s, $encoding, 'UTF-8');
  456. }
  457. return $s;
  458. }
  459. public static function mb_ord($s, $encoding = null)
  460. {
  461. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  462. $s = mb_convert_encoding($s, 'UTF-8', $encoding);
  463. }
  464. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  465. if (0xF0 <= $code) {
  466. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  467. }
  468. if (0xE0 <= $code) {
  469. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  470. }
  471. if (0xC0 <= $code) {
  472. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  473. }
  474. return $code;
  475. }
  476. private static function getSubpart($pos, $part, $haystack, $encoding)
  477. {
  478. if (false === $pos) {
  479. return false;
  480. }
  481. if ($part) {
  482. return self::mb_substr($haystack, 0, $pos, $encoding);
  483. }
  484. return self::mb_substr($haystack, $pos, null, $encoding);
  485. }
  486. private static function html_encoding_callback($m)
  487. {
  488. $i = 1;
  489. $entities = '';
  490. $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
  491. while (isset($m[$i])) {
  492. if (0x80 > $m[$i]) {
  493. $entities .= chr($m[$i++]);
  494. continue;
  495. }
  496. if (0xF0 <= $m[$i]) {
  497. $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  498. } elseif (0xE0 <= $m[$i]) {
  499. $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  500. } else {
  501. $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
  502. }
  503. $entities .= '&#'.$c.';';
  504. }
  505. return $entities;
  506. }
  507. private static function title_case_lower($s)
  508. {
  509. return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8');
  510. }
  511. private static function title_case_upper($s)
  512. {
  513. return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8');
  514. }
  515. private static function getData($file)
  516. {
  517. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
  518. return require $file;
  519. }
  520. return false;
  521. }
  522. private static function getEncoding($encoding)
  523. {
  524. if (null === $encoding) {
  525. return self::$internalEncoding;
  526. }
  527. $encoding = strtoupper($encoding);
  528. if ('8BIT' === $encoding || 'BINARY' === $encoding) {
  529. return 'CP850';
  530. }
  531. if ('UTF8' === $encoding) {
  532. return 'UTF-8';
  533. }
  534. return $encoding;
  535. }
  536. }