Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Defines string apis
  19   *
  20   * @package    core
  21   * @copyright  (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * defines string api's for manipulating strings
  29   *
  30   * This class is used to manipulate strings under Moodle 1.6 an later. As
  31   * utf-8 text become mandatory a pool of safe functions under this encoding
  32   * become necessary. The name of the methods is exactly the
  33   * same than their PHP originals.
  34   *
  35   * A big part of this class acts as a wrapper over the Typo3 charset library,
  36   * really a cool group of utilities to handle texts and encoding conversion.
  37   *
  38   * Take a look to its own copyright and license details.
  39   *
  40   * IMPORTANT Note: Typo3 libraries always expect lowercase charsets to use 100%
  41   * its capabilities so, don't forget to make the conversion
  42   * from every wrapper function!
  43   *
  44   * @package   core
  45   * @category  string
  46   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  47   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class core_text {
  50      /** @var string Byte order mark for UTF-8 */
  51      const UTF8_BOM = "\xef\xbb\xbf";
  52  
  53      /**
  54       * @var string[] Array of strings representing Unicode non-characters
  55       */
  56      protected static $noncharacters;
  57  
  58      /**
  59       * Return t3lib helper class, which is used for conversion between charsets
  60       *
  61       * @param bool $reset
  62       * @return t3lib_cs
  63       */
  64      protected static function typo3($reset = false) {
  65          static $typo3cs = null;
  66  
  67          if ($reset) {
  68              $typo3cs = null;
  69              return null;
  70          }
  71  
  72          if (isset($typo3cs)) {
  73              return $typo3cs;
  74          }
  75  
  76          global $CFG;
  77  
  78          // Required files
  79          require_once($CFG->libdir.'/typo3/class.t3lib_cs.php');
  80          require_once($CFG->libdir.'/typo3/class.t3lib_div.php');
  81          require_once($CFG->libdir.'/typo3/interface.t3lib_singleton.php');
  82          require_once($CFG->libdir.'/typo3/class.t3lib_l10n_locales.php');
  83  
  84          // do not use mbstring or recode because it may return invalid results in some corner cases
  85          $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_convMethod'] = 'iconv';
  86          $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] = 'iconv';
  87  
  88          // Tell Typo3 we are curl enabled always (mandatory since 2.0)
  89          $GLOBALS['TYPO3_CONF_VARS']['SYS']['curlUse'] = '1';
  90  
  91          // And this directory must exist to allow Typo to cache conversion
  92          // tables when using internal functions
  93          make_temp_directory('typo3temp/cs');
  94  
  95          // Make sure typo is using our dir permissions
  96          $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = decoct($CFG->directorypermissions);
  97  
  98          // Default mask for Typo
  99          $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = decoct($CFG->filepermissions);
 100  
 101          // This full path constants must be defined too, transforming backslashes
 102          // to forward slashed because Typo3 requires it.
 103          if (!defined('PATH_t3lib')) {
 104              define('PATH_t3lib', str_replace('\\','/',$CFG->libdir.'/typo3/'));
 105              define('PATH_typo3', str_replace('\\','/',$CFG->libdir.'/typo3/'));
 106              define('PATH_site', str_replace('\\','/',$CFG->tempdir.'/'));
 107              define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
 108          }
 109  
 110          $typo3cs = new t3lib_cs();
 111  
 112          return $typo3cs;
 113      }
 114  
 115      /**
 116       * Reset internal textlib caches.
 117       * @static
 118       */
 119      public static function reset_caches() {
 120          self::typo3(true);
 121      }
 122  
 123      /**
 124       * Standardise charset name
 125       *
 126       * Please note it does not mean the returned charset is actually supported.
 127       *
 128       * @static
 129       * @param string $charset raw charset name
 130       * @return string normalised lowercase charset name
 131       */
 132      public static function parse_charset($charset) {
 133          $charset = strtolower($charset);
 134  
 135          // shortcuts so that we do not have to load typo3 on every page
 136  
 137          if ($charset === 'utf8' or $charset === 'utf-8') {
 138              return 'utf-8';
 139          }
 140  
 141          if (preg_match('/^(cp|win|windows)-?(12[0-9]{2})$/', $charset, $matches)) {
 142              return 'windows-'.$matches[2];
 143          }
 144  
 145          if (preg_match('/^iso-8859-[0-9]+$/', $charset, $matches)) {
 146              return $charset;
 147          }
 148  
 149          if ($charset === 'euc-jp') {
 150              return 'euc-jp';
 151          }
 152          if ($charset === 'iso-2022-jp') {
 153              return 'iso-2022-jp';
 154          }
 155          if ($charset === 'shift-jis' or $charset === 'shift_jis') {
 156              return 'shift_jis';
 157          }
 158          if ($charset === 'gb2312') {
 159              return 'gb2312';
 160          }
 161          if ($charset === 'gb18030') {
 162              return 'gb18030';
 163          }
 164  
 165          // fallback to typo3
 166          return self::typo3()->parse_charset($charset);
 167      }
 168  
 169      /**
 170       * Converts the text between different encodings. It uses iconv extension with //TRANSLIT parameter,
 171       * falls back to typo3. If both source and target are utf-8 it tries to fix invalid characters only.
 172       *
 173       * @param string $text
 174       * @param string $fromCS source encoding
 175       * @param string $toCS result encoding
 176       * @return string|bool converted string or false on error
 177       */
 178      public static function convert($text, $fromCS, $toCS='utf-8') {
 179          $fromCS = self::parse_charset($fromCS);
 180          $toCS   = self::parse_charset($toCS);
 181  
 182          $text = (string)$text; // we can work only with strings
 183  
 184          if ($text === '') {
 185              return '';
 186          }
 187  
 188          if ($fromCS === 'utf-8') {
 189              $text = fix_utf8($text);
 190              if ($toCS === 'utf-8') {
 191                  return $text;
 192              }
 193          }
 194  
 195          if ($toCS === 'ascii') {
 196              // Try to normalize the conversion a bit.
 197              $text = self::specialtoascii($text, $fromCS);
 198          }
 199  
 200          // Prevent any error notices, do not use //IGNORE so that we get
 201          // consistent result from Typo3 if iconv fails.
 202          $result = @iconv($fromCS, $toCS.'//TRANSLIT', $text);
 203  
 204          if ($result === false or $result === '') {
 205              // note: iconv is prone to return empty string when invalid char encountered, or false if encoding unsupported
 206              $oldlevel = error_reporting(E_PARSE);
 207              $result = self::typo3()->conv((string)$text, $fromCS, $toCS);
 208              error_reporting($oldlevel);
 209          }
 210  
 211          return $result;
 212      }
 213  
 214      /**
 215       * Multibyte safe substr() function, uses mbstring or iconv for UTF-8, falls back to typo3.
 216       *
 217       * @param string $text string to truncate
 218       * @param int $start negative value means from end
 219       * @param int $len maximum length of characters beginning from start
 220       * @param string $charset encoding of the text
 221       * @return string portion of string specified by the $start and $len
 222       */
 223      public static function substr($text, $start, $len=null, $charset='utf-8') {
 224          $charset = self::parse_charset($charset);
 225  
 226          if ($charset === 'utf-8') {
 227              if (function_exists('mb_substr')) {
 228                  // this is much faster than iconv - see MDL-31142
 229                  if ($len === null) {
 230                      $oldcharset = mb_internal_encoding();
 231                      mb_internal_encoding('UTF-8');
 232                      $result = mb_substr($text, $start);
 233                      mb_internal_encoding($oldcharset);
 234                      return $result;
 235                  } else {
 236                      return mb_substr($text, $start, $len, 'UTF-8');
 237                  }
 238  
 239              } else {
 240                  if ($len === null) {
 241                      $len = iconv_strlen($text, 'UTF-8');
 242                  }
 243                  return iconv_substr($text, $start, $len, 'UTF-8');
 244              }
 245          }
 246  
 247          $oldlevel = error_reporting(E_PARSE);
 248          if ($len === null) {
 249              $result = self::typo3()->substr($charset, (string)$text, $start);
 250          } else {
 251              $result = self::typo3()->substr($charset, (string)$text, $start, $len);
 252          }
 253          error_reporting($oldlevel);
 254  
 255          return $result;
 256      }
 257  
 258      /**
 259       * Truncates a string to no more than a certain number of bytes in a multi-byte safe manner.
 260       * UTF-8 only!
 261       *
 262       * Many of the other charsets we test for (like ISO-2022-JP and EUC-JP) are not supported
 263       * by typo3, and will give invalid results, so we are supporting UTF-8 only.
 264       *
 265       * @param string $string String to truncate
 266       * @param int $bytes Maximum length of bytes in the result
 267       * @return string Portion of string specified by $bytes
 268       * @since Moodle 3.1
 269       */
 270      public static function str_max_bytes($string, $bytes) {
 271          if (function_exists('mb_strcut')) {
 272              return mb_strcut($string, 0, $bytes, 'UTF-8');
 273          }
 274  
 275          $oldlevel = error_reporting(E_PARSE);
 276          $result = self::typo3()->strtrunc('utf-8', $string, $bytes);
 277          error_reporting($oldlevel);
 278  
 279          return $result;
 280      }
 281  
 282      /**
 283       * Finds the last occurrence of a character in a string within another.
 284       * UTF-8 ONLY safe mb_strrchr().
 285       *
 286       * @param string $haystack The string from which to get the last occurrence of needle.
 287       * @param string $needle The string to find in haystack.
 288       * @param boolean $part If true, returns the portion before needle, else return the portion after (including needle).
 289       * @return string|false False when not found.
 290       * @since Moodle 2.4.6, 2.5.2, 2.6
 291       */
 292      public static function strrchr($haystack, $needle, $part = false) {
 293  
 294          if (function_exists('mb_strrchr')) {
 295              return mb_strrchr($haystack, $needle, $part, 'UTF-8');
 296          }
 297  
 298          $pos = self::strrpos($haystack, $needle);
 299          if ($pos === false) {
 300              return false;
 301          }
 302  
 303          $length = null;
 304          if ($part) {
 305              $length = $pos;
 306              $pos = 0;
 307          }
 308  
 309          return self::substr($haystack, $pos, $length, 'utf-8');
 310      }
 311  
 312      /**
 313       * Multibyte safe strlen() function, uses mbstring or iconv for UTF-8, falls back to typo3.
 314       *
 315       * @param string $text input string
 316       * @param string $charset encoding of the text
 317       * @return int number of characters
 318       */
 319      public static function strlen($text, $charset='utf-8') {
 320          $charset = self::parse_charset($charset);
 321  
 322          if ($charset === 'utf-8') {
 323              if (function_exists('mb_strlen')) {
 324                  return mb_strlen($text, 'UTF-8');
 325              } else {
 326                  return iconv_strlen($text, 'UTF-8');
 327              }
 328          }
 329  
 330          $oldlevel = error_reporting(E_PARSE);
 331          $result = self::typo3()->strlen($charset, (string)$text);
 332          error_reporting($oldlevel);
 333  
 334          return $result;
 335      }
 336  
 337      /**
 338       * Multibyte safe strtolower() function, uses mbstring, falls back to typo3.
 339       *
 340       * @param string $text input string
 341       * @param string $charset encoding of the text (may not work for all encodings)
 342       * @return string lower case text
 343       */
 344      public static function strtolower($text, $charset='utf-8') {
 345          $charset = self::parse_charset($charset);
 346  
 347          if ($charset === 'utf-8' and function_exists('mb_strtolower')) {
 348              return mb_strtolower($text, 'UTF-8');
 349          }
 350  
 351          $oldlevel = error_reporting(E_PARSE);
 352          $result = self::typo3()->conv_case($charset, (string)$text, 'toLower');
 353          error_reporting($oldlevel);
 354  
 355          return $result;
 356      }
 357  
 358      /**
 359       * Multibyte safe strtoupper() function, uses mbstring, falls back to typo3.
 360       *
 361       * @param string $text input string
 362       * @param string $charset encoding of the text (may not work for all encodings)
 363       * @return string upper case text
 364       */
 365      public static function strtoupper($text, $charset='utf-8') {
 366          $charset = self::parse_charset($charset);
 367  
 368          if ($charset === 'utf-8' and function_exists('mb_strtoupper')) {
 369              return mb_strtoupper($text, 'UTF-8');
 370          }
 371  
 372          $oldlevel = error_reporting(E_PARSE);
 373          $result = self::typo3()->conv_case($charset, (string)$text, 'toUpper');
 374          error_reporting($oldlevel);
 375  
 376          return $result;
 377      }
 378  
 379      /**
 380       * Find the position of the first occurrence of a substring in a string.
 381       * UTF-8 ONLY safe strpos(), uses mbstring, falls back to iconv.
 382       *
 383       * @param string $haystack the string to search in
 384       * @param string $needle one or more charachters to search for
 385       * @param int $offset offset from begining of string
 386       * @return int the numeric position of the first occurrence of needle in haystack.
 387       */
 388      public static function strpos($haystack, $needle, $offset=0) {
 389          if (function_exists('mb_strpos')) {
 390              return mb_strpos($haystack, $needle, $offset, 'UTF-8');
 391          } else {
 392              return iconv_strpos($haystack, $needle, $offset, 'UTF-8');
 393          }
 394      }
 395  
 396      /**
 397       * Find the position of the last occurrence of a substring in a string
 398       * UTF-8 ONLY safe strrpos(), uses mbstring, falls back to iconv.
 399       *
 400       * @param string $haystack the string to search in
 401       * @param string $needle one or more charachters to search for
 402       * @return int the numeric position of the last occurrence of needle in haystack
 403       */
 404      public static function strrpos($haystack, $needle) {
 405          if (function_exists('mb_strrpos')) {
 406              return mb_strrpos($haystack, $needle, null, 'UTF-8');
 407          } else {
 408              return iconv_strrpos($haystack, $needle, 'UTF-8');
 409          }
 410      }
 411  
 412      /**
 413       * Reverse UTF-8 multibytes character sets (used for RTL languages)
 414       * (We only do this because there is no mb_strrev or iconv_strrev)
 415       *
 416       * @param string $str the multibyte string to reverse
 417       * @return string the reversed multi byte string
 418       */
 419      public static function strrev($str) {
 420          preg_match_all('/./us', $str, $ar);
 421          return join('', array_reverse($ar[0]));
 422      }
 423  
 424      /**
 425       * Try to convert upper unicode characters to plain ascii,
 426       * the returned string may contain unconverted unicode characters.
 427       *
 428       * @param string $text input string
 429       * @param string $charset encoding of the text
 430       * @return string converted ascii string
 431       */
 432      public static function specialtoascii($text, $charset='utf-8') {
 433          $charset = self::parse_charset($charset);
 434          $oldlevel = error_reporting(E_PARSE);
 435          $result = self::typo3()->specCharsToASCII($charset, (string)$text);
 436          error_reporting($oldlevel);
 437          return $result;
 438      }
 439  
 440      /**
 441       * Generate a correct base64 encoded header to be used in MIME mail messages.
 442       * This function seems to be 100% compliant with RFC1342. Credits go to:
 443       * paravoid (http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283).
 444       *
 445       * @param string $text input string
 446       * @param string $charset encoding of the text
 447       * @return string base64 encoded header
 448       */
 449      public static function encode_mimeheader($text, $charset='utf-8') {
 450          if (empty($text)) {
 451              return (string)$text;
 452          }
 453          // Normalize charset
 454          $charset = self::parse_charset($charset);
 455          // If the text is pure ASCII, we don't need to encode it
 456          if (self::convert($text, $charset, 'ascii') == $text) {
 457              return $text;
 458          }
 459          // Although RFC says that line feed should be \r\n, it seems that
 460          // some mailers double convert \r, so we are going to use \n alone
 461          $linefeed="\n";
 462          // Define start and end of every chunk
 463          $start = "=?$charset?B?";
 464          $end = "?=";
 465          // Accumulate results
 466          $encoded = '';
 467          // Max line length is 75 (including start and end)
 468          $length = 75 - strlen($start) - strlen($end);
 469          // Multi-byte ratio
 470          $multilength = self::strlen($text, $charset);
 471          // Detect if strlen and friends supported
 472          if ($multilength === false) {
 473              if ($charset == 'GB18030' or $charset == 'gb18030') {
 474                  while (strlen($text)) {
 475                      // try to encode first 22 chars - we expect most chars are two bytes long
 476                      if (preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,22}/m', $text, $matches)) {
 477                          $chunk = $matches[0];
 478                          $encchunk = base64_encode($chunk);
 479                          if (strlen($encchunk) > $length) {
 480                              // find first 11 chars - each char in 4 bytes - worst case scenario
 481                              preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,11}/m', $text, $matches);
 482                              $chunk = $matches[0];
 483                              $encchunk = base64_encode($chunk);
 484                          }
 485                          $text = substr($text, strlen($chunk));
 486                          $encoded .= ' '.$start.$encchunk.$end.$linefeed;
 487                      } else {
 488                          break;
 489                      }
 490                  }
 491                  $encoded = trim($encoded);
 492                  return $encoded;
 493              } else {
 494                  return false;
 495              }
 496          }
 497          $ratio = $multilength / strlen($text);
 498          // Base64 ratio
 499          $magic = $avglength = floor(3 * $length * $ratio / 4);
 500          // basic infinite loop protection
 501          $maxiterations = strlen($text)*2;
 502          $iteration = 0;
 503          // Iterate over the string in magic chunks
 504          for ($i=0; $i <= $multilength; $i+=$magic) {
 505              if ($iteration++ > $maxiterations) {
 506                  return false; // probably infinite loop
 507              }
 508              $magic = $avglength;
 509              $offset = 0;
 510              // Ensure the chunk fits in length, reducing magic if necessary
 511              do {
 512                  $magic -= $offset;
 513                  $chunk = self::substr($text, $i, $magic, $charset);
 514                  $chunk = base64_encode($chunk);
 515                  $offset++;
 516              } while (strlen($chunk) > $length);
 517              // This chunk doesn't break any multi-byte char. Use it.
 518              if ($chunk)
 519                  $encoded .= ' '.$start.$chunk.$end.$linefeed;
 520          }
 521          // Strip the first space and the last linefeed
 522          $encoded = substr($encoded, 1, -strlen($linefeed));
 523  
 524          return $encoded;
 525      }
 526  
 527      /**
 528       * Returns HTML entity transliteration table.
 529       * @return array with (html entity => utf-8) elements
 530       */
 531      protected static function get_entities_table() {
 532          static $trans_tbl = null;
 533  
 534          // Generate/create $trans_tbl
 535          if (!isset($trans_tbl)) {
 536              if (version_compare(phpversion(), '5.3.4') < 0) {
 537                  $trans_tbl = array();
 538                  foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key) {
 539                      $trans_tbl[$key] = self::convert($val, 'ISO-8859-1', 'utf-8');
 540                  }
 541  
 542              } else if (version_compare(phpversion(), '5.4.0') < 0) {
 543                  $trans_tbl = get_html_translation_table(HTML_ENTITIES, ENT_COMPAT, 'UTF-8');
 544                  $trans_tbl = array_flip($trans_tbl);
 545  
 546              } else {
 547                  $trans_tbl = get_html_translation_table(HTML_ENTITIES, ENT_COMPAT | ENT_HTML401, 'UTF-8');
 548                  $trans_tbl = array_flip($trans_tbl);
 549              }
 550          }
 551  
 552          return $trans_tbl;
 553      }
 554  
 555      /**
 556       * Converts all the numeric entities &#nnnn; or &#xnnn; to UTF-8
 557       * Original from laurynas dot butkus at gmail at:
 558       * http://php.net/manual/en/function.html-entity-decode.php#75153
 559       * with some custom mods to provide more functionality
 560       *
 561       * @param string $str input string
 562       * @param boolean $htmlent convert also html entities (defaults to true)
 563       * @return string encoded UTF-8 string
 564       */
 565      public static function entities_to_utf8($str, $htmlent=true) {
 566          static $callback1 = null ;
 567          static $callback2 = null ;
 568  
 569          if (!$callback1 or !$callback2) {
 570              $callback1 = function($matches) {
 571                  return core_text::code2utf8(hexdec($matches[1]));
 572              };
 573              $callback2 = function($matches) {
 574                  return core_text::code2utf8($matches[1]);
 575              };
 576          }
 577  
 578          $result = (string)$str;
 579          $result = preg_replace_callback('/&#x([0-9a-f]+);/i', $callback1, $result);
 580          $result = preg_replace_callback('/&#([0-9]+);/', $callback2, $result);
 581  
 582          // Replace literal entities (if desired)
 583          if ($htmlent) {
 584              $trans_tbl = self::get_entities_table();
 585              // It should be safe to search for ascii strings and replace them with utf-8 here.
 586              $result = strtr($result, $trans_tbl);
 587          }
 588          // Return utf8-ised string
 589          return $result;
 590      }
 591  
 592      /**
 593       * Converts all Unicode chars > 127 to numeric entities &#nnnn; or &#xnnn;.
 594       *
 595       * @param string $str input string
 596       * @param boolean $dec output decadic only number entities
 597       * @param boolean $nonnum remove all non-numeric entities
 598       * @return string converted string
 599       */
 600      public static function utf8_to_entities($str, $dec=false, $nonnum=false) {
 601          static $callback = null ;
 602  
 603          if ($nonnum) {
 604              $str = self::entities_to_utf8($str, true);
 605          }
 606  
 607          // Avoid some notices from Typo3 code
 608          $oldlevel = error_reporting(E_PARSE);
 609          $result = self::typo3()->utf8_to_entities((string)$str);
 610          error_reporting($oldlevel);
 611  
 612          if ($dec) {
 613              if (!$callback) {
 614                  $callback = function($matches) {
 615                      return '&#' . hexdec($matches[1]) . ';';
 616                  };
 617              }
 618              $result = preg_replace_callback('/&#x([0-9a-f]+);/i', $callback, $result);
 619          }
 620  
 621          return $result;
 622      }
 623  
 624      /**
 625       * Removes the BOM from unicode string {@link http://unicode.org/faq/utf_bom.html}
 626       *
 627       * @param string $str input string
 628       * @return string
 629       */
 630      public static function trim_utf8_bom($str) {
 631          $bom = self::UTF8_BOM;
 632          if (strpos($str, $bom) === 0) {
 633              return substr($str, strlen($bom));
 634          }
 635          return $str;
 636      }
 637  
 638      /**
 639       * There are a number of Unicode non-characters including the byte-order mark (which may appear
 640       * multiple times in a string) and also other ranges. These can cause problems for some
 641       * processing.
 642       *
 643       * This function removes the characters using string replace, so that the rest of the string
 644       * remains unchanged.
 645       *
 646       * @param string $value Input string
 647       * @return string Cleaned string value
 648       * @since Moodle 3.5
 649       */
 650      public static function remove_unicode_non_characters($value) {
 651          // Set up list of all Unicode non-characters for fast replacing.
 652          if (!self::$noncharacters) {
 653              self::$noncharacters = [];
 654              // This list of characters is based on the Unicode standard. It includes the last two
 655              // characters of each code planes 0-16 inclusive...
 656              for ($plane = 0; $plane <= 16; $plane++) {
 657                  $base = ($plane === 0 ? '' : dechex($plane));
 658                  self::$noncharacters[] = html_entity_decode('&#x' . $base . 'fffe;');
 659                  self::$noncharacters[] = html_entity_decode('&#x' . $base . 'ffff;');
 660              }
 661              // ...And the character range U+FDD0 to U+FDEF.
 662              for ($char = 0xfdd0; $char <= 0xfdef; $char++) {
 663                  self::$noncharacters[] = html_entity_decode('&#x' . dechex($char) . ';');
 664              }
 665          }
 666  
 667          // Do character replacement.
 668          return str_replace(self::$noncharacters, '', $value);
 669      }
 670  
 671      /**
 672       * Returns encoding options for select boxes, utf-8 and platform encoding first
 673       *
 674       * @return array encodings
 675       */
 676      public static function get_encodings() {
 677          $encodings = array();
 678          $encodings['UTF-8'] = 'UTF-8';
 679          $winenc = strtoupper(get_string('localewincharset', 'langconfig'));
 680          if ($winenc != '') {
 681              $encodings[$winenc] = $winenc;
 682          }
 683          $nixenc = strtoupper(get_string('oldcharset', 'langconfig'));
 684          $encodings[$nixenc] = $nixenc;
 685  
 686          foreach (self::typo3()->synonyms as $enc) {
 687              $enc = strtoupper($enc);
 688              $encodings[$enc] = $enc;
 689          }
 690          return $encodings;
 691      }
 692  
 693      /**
 694       * Returns the utf8 string corresponding to the unicode value
 695       * (from php.net, courtesy - romans@void.lv)
 696       *
 697       * @param  int    $num one unicode value
 698       * @return string the UTF-8 char corresponding to the unicode value
 699       */
 700      public static function code2utf8($num) {
 701          if ($num < 128) {
 702              return chr($num);
 703          }
 704          if ($num < 2048) {
 705              return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
 706          }
 707          if ($num < 65536) {
 708              return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
 709          }
 710          if ($num < 2097152) {
 711              return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
 712          }
 713          return '';
 714      }
 715  
 716      /**
 717       * Returns the code of the given UTF-8 character
 718       *
 719       * @param  string $utf8char one UTF-8 character
 720       * @return int    the code of the given character
 721       */
 722      public static function utf8ord($utf8char) {
 723          if ($utf8char == '') {
 724              return 0;
 725          }
 726          $ord0 = ord($utf8char[0]);
 727          if ($ord0 >= 0 && $ord0 <= 127) {
 728              return $ord0;
 729          }
 730          $ord1 = ord($utf8char[1]);
 731          if ($ord0 >= 192 && $ord0 <= 223) {
 732              return ($ord0 - 192) * 64 + ($ord1 - 128);
 733          }
 734          $ord2 = ord($utf8char[2]);
 735          if ($ord0 >= 224 && $ord0 <= 239) {
 736              return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
 737          }
 738          $ord3 = ord($utf8char[3]);
 739          if ($ord0 >= 240 && $ord0 <= 247) {
 740              return ($ord0 - 240) * 262144 + ($ord1 - 128 )* 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
 741          }
 742          return false;
 743      }
 744  
 745      /**
 746       * Makes first letter of each word capital - words must be separated by spaces.
 747       * Use with care, this function does not work properly in many locales!!!
 748       *
 749       * @param string $text input string
 750       * @return string
 751       */
 752      public static function strtotitle($text) {
 753          if (empty($text)) {
 754              return $text;
 755          }
 756  
 757          if (function_exists('mb_convert_case')) {
 758              return mb_convert_case($text, MB_CASE_TITLE, 'UTF-8');
 759          }
 760  
 761          $text = self::strtolower($text);
 762          $words = explode(' ', $text);
 763          foreach ($words as $i=>$word) {
 764              $length = self::strlen($word);
 765              if (!$length) {
 766                  continue;
 767  
 768              } else if ($length == 1) {
 769                  $words[$i] = self::strtoupper($word);
 770  
 771              } else {
 772                  $letter = self::substr($word, 0, 1);
 773                  $letter = self::strtoupper($letter);
 774                  $rest   = self::substr($word, 1);
 775                  $words[$i] = $letter.$rest;
 776              }
 777          }
 778          return implode(' ', $words);
 779      }
 780  }