Merge branch 'MDL-81713-main' of https://github.com/junpataleta/moodle
[moodle.git] / lib / html2text / override.php
blob46bea72230c9fcfaff6ea511a0732056004e5824
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.
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/>.
17 /**
18 * Run time overrides for Html2Text
20 * This allows us to monkey patch the mb_* functions used in Html2Text to use Moodle's core_text functionality.
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
27 namespace Html2Text;
29 /**
30 * Set the encoding to be used by our monkey patched mb_ functions.
32 * When called with $encoding !== null, we set the static $intenalencoding
33 * variable, which is used for subsequent calls.
35 * When called with no value for $encoding, we return the previously defined
36 * $internalencoding.
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.
43 * @staticvar string $internalencoding The encoding to be used across mb_* calls.
44 * @param string $encoding When given, sets $internalencoding
45 * @return mixed
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;
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.
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.
75 * @return string The portion of str specified by the start and length parameters.
77 function mb_substr($str, $start, $length = null, $encoding = null) {
78 if ($encoding === null) {
79 $encoding = mb_internal_encoding();
81 return \core_text::substr($str, $start, $length, $encoding);
84 /**
85 * Gets the length of a string.
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.
92 * @return int The number of characters in str having character encoding $encoding.
93 * A multibyte character is counted as 1.
95 function mb_strlen($str, $encoding = null) {
96 if ($encoding === null) {
97 $encoding = mb_internal_encoding();
99 return \core_text::strlen($str, $encoding);
103 * Returns $str with all alphabetic chatacters converted to lowercase.
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.
110 * @return string The string with all alphabetic characters converted to lowercase.
112 function mb_strtolower($str, $encoding = null) {
113 if ($encoding === null) {
114 $encoding = mb_internal_encoding();
116 return \core_text::strtolower($str, $encoding);
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.
126 * @return string The string with all alphabetic characters converted to uppercase.
128 function mb_strtoupper($str, $encoding = null) {
129 if ($encoding === null) {
130 $encoding = mb_internal_encoding();
132 return \core_text::strtoupper($str, $encoding);