PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/classes/text.php

https://github.com/dongsheng/moodle
PHP | 672 lines | 348 code | 62 blank | 262 comment | 62 complexity | 837c772089ebdef82f56572305b04954 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Defines string apis
  18. *
  19. * @package core
  20. * @copyright (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * defines string api's for manipulating strings
  26. *
  27. * This class is used to manipulate strings under Moodle 1.6 an later. As
  28. * utf-8 text become mandatory a pool of safe functions under this encoding
  29. * become necessary. The name of the methods is exactly the
  30. * same than their PHP originals.
  31. *
  32. * This class was previously based on Typo3 which has now been removed and uses
  33. * native functions now.
  34. *
  35. * @package core
  36. * @category string
  37. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  38. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39. */
  40. class core_text {
  41. /** @var string Byte order mark for UTF-8 */
  42. const UTF8_BOM = "\xef\xbb\xbf";
  43. /**
  44. * @var string[] Array of strings representing Unicode non-characters
  45. */
  46. protected static $noncharacters;
  47. /**
  48. * Check whether the charset is supported by mbstring.
  49. * @param string $charset Normalised charset
  50. * @return bool
  51. */
  52. public static function is_charset_supported(string $charset): bool {
  53. static $cache = null;
  54. if (!$cache) {
  55. $cache = array_flip(array_map('strtolower', mb_list_encodings()));
  56. }
  57. if (isset($cache[strtolower($charset)])) {
  58. return true;
  59. }
  60. // We haven't found the charset, check if mb has aliases for the charset.
  61. try {
  62. return mb_encoding_aliases($charset) !== false;
  63. } catch (Throwable $e) {
  64. // A ValueError will be thrown if unsupported.
  65. }
  66. return false;
  67. }
  68. /**
  69. * Reset internal textlib caches.
  70. * @static
  71. * @deprecated since Moodle 4.0. See MDL-53544.
  72. * @todo To be removed in Moodle 4.4 - MDL-71748
  73. */
  74. public static function reset_caches() {
  75. debugging("reset_caches() is deprecated. Typo3 has been removed and caches aren't used anymore.", DEBUG_DEVELOPER);
  76. }
  77. /**
  78. * Standardise charset name
  79. *
  80. * Please note it does not mean the returned charset is actually supported.
  81. *
  82. * @static
  83. * @param string $charset raw charset name
  84. * @return string normalised lowercase charset name
  85. */
  86. public static function parse_charset($charset) {
  87. $charset = strtolower($charset);
  88. if ($charset === 'utf8' or $charset === 'utf-8') {
  89. return 'utf-8';
  90. }
  91. if (preg_match('/^(cp|win|windows)-?(12[0-9]{2})$/', $charset, $matches)) {
  92. return 'windows-'.$matches[2];
  93. }
  94. if (preg_match('/^iso-8859-[0-9]+$/', $charset, $matches)) {
  95. return $charset;
  96. }
  97. if ($charset === 'euc-jp') {
  98. return 'euc-jp';
  99. }
  100. if ($charset === 'iso-2022-jp') {
  101. return 'iso-2022-jp';
  102. }
  103. if ($charset === 'shift-jis' or $charset === 'shift_jis') {
  104. return 'shift_jis';
  105. }
  106. if ($charset === 'gb2312') {
  107. return 'gb2312';
  108. }
  109. if ($charset === 'gb18030') {
  110. return 'gb18030';
  111. }
  112. if ($charset === 'ms-ansi') {
  113. return 'windows-1252';
  114. }
  115. // We have reached this stage and haven't matched with anything. Return the original.
  116. return $charset;
  117. }
  118. /**
  119. * Converts the text between different encodings. It uses iconv extension with //TRANSLIT parameter.
  120. * If both source and target are utf-8 it tries to fix invalid characters only.
  121. *
  122. * @param string $text
  123. * @param string $fromCS source encoding
  124. * @param string $toCS result encoding
  125. * @return string|bool converted string or false on error
  126. */
  127. public static function convert($text, $fromCS, $toCS='utf-8') {
  128. $fromCS = self::parse_charset($fromCS);
  129. $toCS = self::parse_charset($toCS);
  130. $text = (string)$text; // we can work only with strings
  131. if ($text === '') {
  132. return '';
  133. }
  134. if ($fromCS === 'utf-8') {
  135. $text = fix_utf8($text);
  136. if ($toCS === 'utf-8') {
  137. return $text;
  138. }
  139. }
  140. if ($toCS === 'ascii') {
  141. // Try to normalize the conversion a bit.
  142. $text = self::specialtoascii($text, $fromCS);
  143. }
  144. // Prevent any error notices, do not use //IGNORE so that we get
  145. // consistent result if iconv fails.
  146. $result = @iconv($fromCS, $toCS.'//TRANSLIT', $text);
  147. if ($result === false or $result === '') {
  148. // Note: iconv is prone to return empty string when invalid char encountered, or false if encoding unsupported.
  149. $oldlevel = error_reporting(E_PARSE);
  150. error_reporting($oldlevel);
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Multibyte safe substr() function, uses mbstring or iconv
  156. *
  157. * @param string $text string to truncate
  158. * @param int $start negative value means from end
  159. * @param int $len maximum length of characters beginning from start
  160. * @param string $charset encoding of the text
  161. * @return string portion of string specified by the $start and $len
  162. */
  163. public static function substr($text, $start, $len=null, $charset='utf-8') {
  164. $charset = self::parse_charset($charset);
  165. // Check whether the charset is supported by mbstring. CP1250 is not supported. Fall back to iconv.
  166. if (self::is_charset_supported($charset)) {
  167. $result = mb_substr($text, $start, $len, $charset);
  168. } else {
  169. $result = iconv_substr($text, $start, $len, $charset);
  170. }
  171. return $result;
  172. }
  173. /**
  174. * Truncates a string to no more than a certain number of bytes in a multi-byte safe manner.
  175. * UTF-8 only!
  176. *
  177. * @param string $string String to truncate
  178. * @param int $bytes Maximum length of bytes in the result
  179. * @return string Portion of string specified by $bytes
  180. * @since Moodle 3.1
  181. */
  182. public static function str_max_bytes($string, $bytes) {
  183. return mb_strcut($string, 0, $bytes, 'UTF-8');
  184. }
  185. /**
  186. * Finds the last occurrence of a character in a string within another.
  187. * UTF-8 ONLY safe mb_strrchr().
  188. *
  189. * @param string $haystack The string from which to get the last occurrence of needle.
  190. * @param string $needle The string to find in haystack.
  191. * @param boolean $part If true, returns the portion before needle, else return the portion after (including needle).
  192. * @return string|false False when not found.
  193. * @since Moodle 2.4.6, 2.5.2, 2.6
  194. */
  195. public static function strrchr($haystack, $needle, $part = false) {
  196. return mb_strrchr($haystack, $needle, $part, 'UTF-8');
  197. }
  198. /**
  199. * Multibyte safe strlen() function, uses mbstring or iconv
  200. *
  201. * @param string $text input string
  202. * @param string $charset encoding of the text
  203. * @return int number of characters
  204. */
  205. public static function strlen($text, $charset='utf-8') {
  206. $charset = self::parse_charset($charset);
  207. if (self::is_charset_supported($charset)) {
  208. return mb_strlen($text, $charset);
  209. }
  210. return iconv_strlen($text, $charset);
  211. }
  212. /**
  213. * Multibyte safe strtolower() function, uses mbstring.
  214. *
  215. * @param string $text input string
  216. * @param string $charset encoding of the text (may not work for all encodings)
  217. * @return string lower case text
  218. */
  219. public static function strtolower($text, $charset='utf-8') {
  220. $charset = self::parse_charset($charset);
  221. // Confirm mbstring can handle the charset.
  222. if (self::is_charset_supported($charset)) {
  223. return mb_strtolower($text, $charset);
  224. }
  225. // The mbstring extension cannot handle the charset. Convert to UTF-8.
  226. $convertedtext = self::convert($text, $charset, 'utf-8');
  227. $result = mb_strtolower($convertedtext);
  228. $result = self::convert($result, 'utf-8', $charset);
  229. return $result;
  230. }
  231. /**
  232. * Multibyte safe strtoupper() function, uses mbstring.
  233. *
  234. * @param string $text input string
  235. * @param string $charset encoding of the text (may not work for all encodings)
  236. * @return string upper case text
  237. */
  238. public static function strtoupper($text, $charset='utf-8') {
  239. $charset = self::parse_charset($charset);
  240. // Confirm mbstring can handle the charset.
  241. if (self::is_charset_supported($charset)) {
  242. return mb_strtoupper($text, $charset);
  243. }
  244. // The mbstring extension cannot handle the charset. Convert to UTF-8.
  245. $convertedtext = self::convert($text, $charset, 'utf-8');
  246. $result = mb_strtoupper($convertedtext);
  247. $result = self::convert($result, 'utf-8', $charset);
  248. return $result;
  249. }
  250. /**
  251. * Find the position of the first occurrence of a substring in a string.
  252. * UTF-8 ONLY safe strpos(), uses mbstring
  253. *
  254. * @param string $haystack the string to search in
  255. * @param string $needle one or more charachters to search for
  256. * @param int $offset offset from begining of string
  257. * @return int the numeric position of the first occurrence of needle in haystack.
  258. */
  259. public static function strpos($haystack, $needle, $offset=0) {
  260. return mb_strpos($haystack, $needle, $offset, 'UTF-8');
  261. }
  262. /**
  263. * Find the position of the last occurrence of a substring in a string
  264. * UTF-8 ONLY safe strrpos(), uses mbstring
  265. *
  266. * @param string $haystack the string to search in
  267. * @param string $needle one or more charachters to search for
  268. * @return int the numeric position of the last occurrence of needle in haystack
  269. */
  270. public static function strrpos($haystack, $needle) {
  271. return mb_strrpos($haystack, $needle, null, 'UTF-8');
  272. }
  273. /**
  274. * Reverse UTF-8 multibytes character sets (used for RTL languages)
  275. * (We only do this because there is no mb_strrev or iconv_strrev)
  276. *
  277. * @param string $str the multibyte string to reverse
  278. * @return string the reversed multi byte string
  279. */
  280. public static function strrev($str) {
  281. preg_match_all('/./us', $str, $ar);
  282. return join('', array_reverse($ar[0]));
  283. }
  284. /**
  285. * Try to convert upper unicode characters to plain ascii,
  286. * the returned string may contain unconverted unicode characters.
  287. *
  288. * With the removal of typo3, iconv conversions was found to be the best alternative to Typo3's function.
  289. * However using the standard iconv call
  290. * iconv($charset, 'ASCII//TRANSLIT//IGNORE', (string) $text);
  291. * resulted in invalid strings with special character from Russian/Japanese. To solve this, the transliterator was
  292. * used but this resulted in empty strings for certain strings in our test. It was decided to use a combo of the 2
  293. * to cover all our bases. Refer MDL-53544 for further information.
  294. *
  295. * @param string $text input string
  296. * @param string $charset encoding of the text
  297. * @return string converted ascii string
  298. */
  299. public static function specialtoascii($text, $charset='utf-8') {
  300. $charset = self::parse_charset($charset);
  301. $oldlevel = error_reporting(E_PARSE);
  302. if ($charset == 'utf-8') {
  303. $text = transliterator_transliterate('Any-Latin; Latin-ASCII', (string) $text);
  304. }
  305. $result = iconv($charset, 'ASCII//TRANSLIT//IGNORE', (string) $text);
  306. error_reporting($oldlevel);
  307. return $result;
  308. }
  309. /**
  310. * Generate a correct base64 encoded header to be used in MIME mail messages.
  311. * This function seems to be 100% compliant with RFC1342. Credits go to:
  312. * paravoid (http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283).
  313. *
  314. * @param string $text input string
  315. * @param string $charset encoding of the text
  316. * @return string base64 encoded header
  317. */
  318. public static function encode_mimeheader($text, $charset='utf-8') {
  319. if (empty($text)) {
  320. return (string)$text;
  321. }
  322. // Normalize charset
  323. $charset = self::parse_charset($charset);
  324. // If the text is pure ASCII, we don't need to encode it
  325. if (self::convert($text, $charset, 'ascii') == $text) {
  326. return $text;
  327. }
  328. // Although RFC says that line feed should be \r\n, it seems that
  329. // some mailers double convert \r, so we are going to use \n alone
  330. $linefeed="\n";
  331. // Define start and end of every chunk
  332. $start = "=?$charset?B?";
  333. $end = "?=";
  334. // Accumulate results
  335. $encoded = '';
  336. // Max line length is 75 (including start and end)
  337. $length = 75 - strlen($start) - strlen($end);
  338. // Multi-byte ratio
  339. $multilength = self::strlen($text, $charset);
  340. // Detect if strlen and friends supported
  341. if ($multilength === false) {
  342. if ($charset == 'GB18030' or $charset == 'gb18030') {
  343. while (strlen($text)) {
  344. // try to encode first 22 chars - we expect most chars are two bytes long
  345. if (preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,22}/m', $text, $matches)) {
  346. $chunk = $matches[0];
  347. $encchunk = base64_encode($chunk);
  348. if (strlen($encchunk) > $length) {
  349. // find first 11 chars - each char in 4 bytes - worst case scenario
  350. preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,11}/m', $text, $matches);
  351. $chunk = $matches[0];
  352. $encchunk = base64_encode($chunk);
  353. }
  354. $text = substr($text, strlen($chunk));
  355. $encoded .= ' '.$start.$encchunk.$end.$linefeed;
  356. } else {
  357. break;
  358. }
  359. }
  360. $encoded = trim($encoded);
  361. return $encoded;
  362. } else {
  363. return false;
  364. }
  365. }
  366. $ratio = $multilength / strlen($text);
  367. // Base64 ratio
  368. $magic = $avglength = floor(3 * $length * $ratio / 4);
  369. // basic infinite loop protection
  370. $maxiterations = strlen($text)*2;
  371. $iteration = 0;
  372. // Iterate over the string in magic chunks
  373. for ($i=0; $i <= $multilength; $i+=$magic) {
  374. if ($iteration++ > $maxiterations) {
  375. return false; // probably infinite loop
  376. }
  377. $magic = $avglength;
  378. $offset = 0;
  379. // Ensure the chunk fits in length, reducing magic if necessary
  380. do {
  381. $magic -= $offset;
  382. $chunk = self::substr($text, $i, $magic, $charset);
  383. $chunk = base64_encode($chunk);
  384. $offset++;
  385. } while (strlen($chunk) > $length);
  386. // This chunk doesn't break any multi-byte char. Use it.
  387. if ($chunk)
  388. $encoded .= ' '.$start.$chunk.$end.$linefeed;
  389. }
  390. // Strip the first space and the last linefeed
  391. $encoded = substr($encoded, 1, -strlen($linefeed));
  392. return $encoded;
  393. }
  394. /**
  395. * Returns HTML entity transliteration table.
  396. * @return array with (html entity => utf-8) elements
  397. */
  398. protected static function get_entities_table() {
  399. static $trans_tbl = null;
  400. // Generate/create $trans_tbl
  401. if (!isset($trans_tbl)) {
  402. if (version_compare(phpversion(), '5.3.4') < 0) {
  403. $trans_tbl = array();
  404. foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key) {
  405. $trans_tbl[$key] = self::convert($val, 'ISO-8859-1', 'utf-8');
  406. }
  407. } else if (version_compare(phpversion(), '5.4.0') < 0) {
  408. $trans_tbl = get_html_translation_table(HTML_ENTITIES, ENT_COMPAT, 'UTF-8');
  409. $trans_tbl = array_flip($trans_tbl);
  410. } else {
  411. $trans_tbl = get_html_translation_table(HTML_ENTITIES, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  412. $trans_tbl = array_flip($trans_tbl);
  413. }
  414. }
  415. return $trans_tbl;
  416. }
  417. /**
  418. * Converts all the numeric entities &#nnnn; or &#xnnn; to UTF-8
  419. * Original from laurynas dot butkus at gmail at:
  420. * http://php.net/manual/en/function.html-entity-decode.php#75153
  421. * with some custom mods to provide more functionality
  422. *
  423. * @param string $str input string
  424. * @param boolean $htmlent convert also html entities (defaults to true)
  425. * @return string encoded UTF-8 string
  426. */
  427. public static function entities_to_utf8($str, $htmlent=true) {
  428. static $callback1 = null ;
  429. static $callback2 = null ;
  430. if (!$callback1 or !$callback2) {
  431. $callback1 = function($matches) {
  432. return core_text::code2utf8(hexdec($matches[1]));
  433. };
  434. $callback2 = function($matches) {
  435. return core_text::code2utf8($matches[1]);
  436. };
  437. }
  438. $result = (string)$str;
  439. $result = preg_replace_callback('/&#x([0-9a-f]+);/i', $callback1, $result);
  440. $result = preg_replace_callback('/&#([0-9]+);/', $callback2, $result);
  441. // Replace literal entities (if desired)
  442. if ($htmlent) {
  443. $trans_tbl = self::get_entities_table();
  444. // It should be safe to search for ascii strings and replace them with utf-8 here.
  445. $result = strtr($result, $trans_tbl);
  446. }
  447. // Return utf8-ised string
  448. return $result;
  449. }
  450. /**
  451. * Converts all Unicode chars > 127 to numeric entities &#nnnn; or &#xnnn;.
  452. *
  453. * @param string $str input string
  454. * @param boolean $dec output decadic only number entities
  455. * @param boolean $nonnum remove all non-numeric entities
  456. * @return string converted string
  457. */
  458. public static function utf8_to_entities($str, $dec=false, $nonnum=false) {
  459. static $callback = null ;
  460. if ($nonnum) {
  461. $str = self::entities_to_utf8($str, true);
  462. }
  463. $result = mb_strtolower(mb_encode_numericentity($str, [0xa0, 0xffff, 0, 0xffff], 'UTF-8', true));
  464. // We cannot use the decimal equivalent of the above call due to the unit test and our allowance for
  465. // entities to be entered within the provided $str. Refer to the correspond unit test for examples.
  466. if ($dec) {
  467. if (!$callback) {
  468. $callback = function($matches) {
  469. return '&#' . hexdec($matches[1]) . ';';
  470. };
  471. }
  472. $result = preg_replace_callback('/&#x([0-9a-f]+);/i', $callback, $result);
  473. }
  474. return $result;
  475. }
  476. /**
  477. * Removes the BOM from unicode string {@link http://unicode.org/faq/utf_bom.html}
  478. *
  479. * @param string $str input string
  480. * @return string
  481. */
  482. public static function trim_utf8_bom($str) {
  483. $bom = self::UTF8_BOM;
  484. if (strpos($str, $bom) === 0) {
  485. return substr($str, strlen($bom));
  486. }
  487. return $str;
  488. }
  489. /**
  490. * There are a number of Unicode non-characters including the byte-order mark (which may appear
  491. * multiple times in a string) and also other ranges. These can cause problems for some
  492. * processing.
  493. *
  494. * This function removes the characters using string replace, so that the rest of the string
  495. * remains unchanged.
  496. *
  497. * @param string $value Input string
  498. * @return string Cleaned string value
  499. * @since Moodle 3.5
  500. */
  501. public static function remove_unicode_non_characters($value) {
  502. // Set up list of all Unicode non-characters for fast replacing.
  503. if (!self::$noncharacters) {
  504. self::$noncharacters = [];
  505. // This list of characters is based on the Unicode standard. It includes the last two
  506. // characters of each code planes 0-16 inclusive...
  507. for ($plane = 0; $plane <= 16; $plane++) {
  508. $base = ($plane === 0 ? '' : dechex($plane));
  509. self::$noncharacters[] = html_entity_decode('&#x' . $base . 'fffe;');
  510. self::$noncharacters[] = html_entity_decode('&#x' . $base . 'ffff;');
  511. }
  512. // ...And the character range U+FDD0 to U+FDEF.
  513. for ($char = 0xfdd0; $char <= 0xfdef; $char++) {
  514. self::$noncharacters[] = html_entity_decode('&#x' . dechex($char) . ';');
  515. }
  516. }
  517. // Do character replacement.
  518. return str_replace(self::$noncharacters, '', $value);
  519. }
  520. /**
  521. * Returns encoding options for select boxes, utf-8 and platform encoding first
  522. *
  523. * @return array encodings
  524. */
  525. public static function get_encodings() {
  526. $encodings = array();
  527. $encodings['UTF-8'] = 'UTF-8';
  528. $winenc = strtoupper(get_string('localewincharset', 'langconfig'));
  529. if ($winenc != '') {
  530. $encodings[$winenc] = $winenc;
  531. }
  532. $nixenc = strtoupper(get_string('oldcharset', 'langconfig'));
  533. $encodings[$nixenc] = $nixenc;
  534. $listedencodings = mb_list_encodings();
  535. foreach ($listedencodings as $enc) {
  536. $enc = strtoupper($enc);
  537. $encodings[$enc] = $enc;
  538. }
  539. return $encodings;
  540. }
  541. /**
  542. * Returns the utf8 string corresponding to the unicode value
  543. * (from php.net, courtesy - romans@void.lv)
  544. *
  545. * @param int $num one unicode value
  546. * @return string the UTF-8 char corresponding to the unicode value
  547. */
  548. public static function code2utf8($num) {
  549. if ($num < 128) {
  550. return chr($num);
  551. }
  552. if ($num < 2048) {
  553. return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
  554. }
  555. if ($num < 65536) {
  556. return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  557. }
  558. if ($num < 2097152) {
  559. return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  560. }
  561. return '';
  562. }
  563. /**
  564. * Returns the code of the given UTF-8 character
  565. *
  566. * @param string $utf8char one UTF-8 character
  567. * @return int the code of the given character
  568. */
  569. public static function utf8ord($utf8char) {
  570. if ($utf8char == '') {
  571. return 0;
  572. }
  573. $ord0 = ord($utf8char[0]);
  574. if ($ord0 >= 0 && $ord0 <= 127) {
  575. return $ord0;
  576. }
  577. $ord1 = ord($utf8char[1]);
  578. if ($ord0 >= 192 && $ord0 <= 223) {
  579. return ($ord0 - 192) * 64 + ($ord1 - 128);
  580. }
  581. $ord2 = ord($utf8char[2]);
  582. if ($ord0 >= 224 && $ord0 <= 239) {
  583. return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  584. }
  585. $ord3 = ord($utf8char[3]);
  586. if ($ord0 >= 240 && $ord0 <= 247) {
  587. return ($ord0 - 240) * 262144 + ($ord1 - 128 )* 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
  588. }
  589. return false;
  590. }
  591. /**
  592. * Makes first letter of each word capital - words must be separated by spaces.
  593. * Use with care, this function does not work properly in many locales!!!
  594. *
  595. * @param string $text input string
  596. * @return string
  597. */
  598. public static function strtotitle($text) {
  599. if (empty($text)) {
  600. return $text;
  601. }
  602. return mb_convert_case($text, MB_CASE_TITLE, 'UTF-8');
  603. }
  604. }