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.
   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   * Run time overrides for Html2Text
  19   *
  20   * This allows us to monkey patch the mb_* functions used in Html2Text to use Moodle's core_text functionality.
  21   *
  22   * @package    core
  23   * @copyright  2016 Andrew Nicols <andrew@nicols.uk>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace Html2Text;
  28  
  29  /**
  30   * Set the encoding to be used by our monkey patched mb_ functions.
  31   *
  32   * When called with $encoding !== null, we set  the static $intenalencoding
  33   * variable, which is used for subsequent calls.
  34   *
  35   * When called with no value for $encoding, we return the previously defined
  36   * $internalencoding.
  37   *
  38   * This is necessary as we need to maintain the state of mb_internal_encoding
  39   * across calls to other mb_* functions. Check how it is used in the other mb_*
  40   * functions defined here - if no encoding is provided we fallback to what was
  41   * set here, otherwise we used the given encoding.
  42   *
  43   * @staticvar string $internalencoding The encoding to be used across mb_* calls.
  44   * @param     string $encoding When given, sets $internalencoding
  45   * @return    mixed
  46   */
  47  function mb_internal_encoding($encoding = null) {
  48      static $internalencoding = 'utf-8';
  49      if ($encoding !== null) {
  50          $internalencoding = $encoding;
  51          return true;
  52      } else {
  53          return $internalencoding;
  54      }
  55  }
  56  
  57  /**
  58   * Performs a multi-byte safe substr() operation based on number of characters.
  59   * Position is counted from the beginning of str. First character's position is
  60   * 0. Second character position is 1, and so on.
  61   *
  62   * @param string $str      The string to extract the substring from.
  63   * @param int    $start    If start is non-negative, the returned string will
  64   *                         start at the start'th position in string, counting
  65   *                         from zero. For instance, in the string 'abcdef',
  66   *                         the character at position 0 is 'a', the character
  67   *                         at position 2 is 'c', and so forth.
  68   * @param int    $length   Maximum number of characters to use from str. If
  69   *                         omitted or NULL is passed, extract all characters
  70   *                         to the end of the string.
  71   * @param string $encoding The encoding parameter is the character encoding.
  72   *                         If it is omitted, the internal character encoding
  73   *                         value will be used.
  74   *
  75   * @return string The portion of str specified by the start and length parameters.
  76   */
  77  function mb_substr($str, $start, $length = null, $encoding = null) {
  78      if ($encoding === null) {
  79          $encoding = mb_internal_encoding();
  80      }
  81      return \core_text::substr($str, $start, $length, $encoding);
  82  }
  83  
  84  /**
  85   * Gets the length of a string.
  86   *
  87   * @param string $str      The string being checked for length.
  88   * @param string $encoding The encoding parameter is the character encoding.
  89   *                         If it is omitted, the internal character encoding
  90   *                         value will be used.
  91   *
  92   * @return int The number of characters in str having character encoding $encoding.
  93   *             A multibyte character is counted as 1.
  94   */
  95  function mb_strlen($str, $encoding = null) {
  96      if ($encoding === null) {
  97          $encoding = mb_internal_encoding();
  98      }
  99      return \core_text::strlen($str, $encoding);
 100  }
 101  
 102  /**
 103   * Returns $str with all alphabetic chatacters converted to lowercase.
 104   *
 105   * @param string $str      The string being lowercased.
 106   * @param string $encoding The encoding parameter is the character encoding.
 107   *                         If it is omitted, the internal character encoding
 108   *                         value will be used.
 109   *
 110   * @return string The string with all alphabetic characters converted to lowercase.
 111   */
 112  function mb_strtolower($str, $encoding = null) {
 113      if ($encoding === null) {
 114          $encoding = mb_internal_encoding();
 115      }
 116      return \core_text::strtolower($str, $encoding);
 117  }
 118  
 119  /**
 120   *
 121   * @param string The string being uppercased
 122   * @param string $encoding The encoding parameter is the character encoding.
 123   *                         If it is omitted, the internal character encoding
 124   *                         value will be used.
 125   *
 126   * @return string The string with all alphabetic characters converted to uppercase.
 127   */
 128  function mb_strtoupper($str, $encoding = null) {
 129      if ($encoding === null) {
 130          $encoding = mb_internal_encoding();
 131      }
 132      return \core_text::strtoupper($str, $encoding);
 133  }