1<?php 2// phpcs:ignoreFile 3/** 4 * The main string wrapper class. 5 * 6 * The class uses mbstring extension if available. It's strongly recommended 7 * to use and extend this class instead of using direct string functions. Doing so 8 * you guarantees your code is upwards compatible with UTF-8 improvements. All 9 * the string methods behaviour is identical to that of the same named 10 * single byte string functions. 11 * 12 * This Source Code Form is subject to the terms of the Mozilla Public License, 13 * v. 2.0. If a copy of the MPL was not distributed with this file, You can 14 * obtain one at http://mozilla.org/MPL/2.0/. 15 * 16 * @package phpMyFAQ 17 * @author Anatoliy Belsky <ab@php.net> 18 * @copyright 2009-2020 phpMyFAQ Team 19 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 20 * @link https://www.phpmyfaq.de 21 * @since 2009-04-06 22 */ 23 24namespace phpMyFAQ; 25 26use phpMyFAQ\Strings\Mbstring; 27use phpMyFAQ\Strings\StringBasic; 28 29/** 30 * Class Strings 31 * 32 * @package phpMyFAQ 33 */ 34class Strings 35{ 36 /** 37 * Instance. 38 * 39 * @var Strings 40 */ 41 private static $instance; 42 43 /** 44 * Constructor. 45 */ 46 final private function __construct() 47 { 48 } 49 50 /** 51 * Init. 52 * 53 * @param string $language Language 54 */ 55 public static function init(string $language = 'en') 56 { 57 if (!self::$instance) { 58 if (extension_loaded('mbstring') && function_exists('mb_regex_encoding')) { 59 self::$instance = Mbstring::getInstance($language); 60 } else { 61 self::$instance = StringBasic::getInstance($language); 62 } 63 } 64 } 65 66 /** 67 * Get current encoding. 68 * 69 * @return string 70 */ 71 public static function getEncoding() 72 { 73 return self::$instance->getEncoding(); 74 } 75 76 /** 77 * Get string character count. 78 * 79 * @param string $str String 80 * 81 * @return int 82 */ 83 public static function strlen(string $str): int 84 { 85 return self::$instance->strlen($str); 86 } 87 88 /** 89 * Get a part of string. 90 * 91 * @param string $string String 92 * @param int $start Start 93 * @param int $length Length 94 * 95 * @return string 96 */ 97 public static function substr($string, $start, $length = 0) 98 { 99 return self::$instance->substr($string, $start, $length); 100 } 101 102 /** 103 * Get position of the first occurrence of a string. 104 * 105 * @param string $haystack Haystack 106 * @param string $needle Needle 107 * @param int $offset Offset 108 * 109 * @return int 110 */ 111 public static function strpos(string $haystack, string $needle, $offset = 0): int 112 { 113 return self::$instance->strpos($haystack, $needle, $offset); 114 } 115 116 /** 117 * Make a string lower case. 118 * 119 * @param string $str String 120 * 121 * @return string 122 */ 123 public static function strtolower($str) 124 { 125 return self::$instance->strtolower($str); 126 } 127 128 /** 129 * Make a string upper case. 130 * 131 * @param string $str String 132 * 133 * @return string 134 */ 135 public static function strtoupper($str) 136 { 137 return self::$instance->strtoupper($str); 138 } 139 140 /** 141 * Get occurrence of a string within another. 142 * 143 * @param string $haystack Haystack 144 * @param string $needle Needle 145 * @param bool $part Part 146 * 147 * @return string|false 148 */ 149 public static function strstr($haystack, $needle, $part = false) 150 { 151 return self::$instance->strstr($haystack, $needle, $part); 152 } 153 154 /** 155 * Set current encoding. 156 * 157 * @param string $encoding 158 */ 159 public static function setEncoding($encoding) 160 { 161 self::$instance->setEncoding($encoding); 162 } 163 164 /** 165 * Count substring occurrences. 166 * 167 * @param string $haystack 168 * @param string $needle 169 * 170 * @return int 171 */ 172 public static function substr_count($haystack, $needle) // phpcs:ignore 173 { 174 return self::$instance->substr_count($haystack, $needle); 175 } 176 177 /** 178 * Find position of last occurrence of a char in a string. 179 * 180 * @param string $haystack 181 * @param string $needle 182 * @param int $offset 183 * 184 * @return int 185 */ 186 public static function strrpos($haystack, $needle, $offset = 0) 187 { 188 return self::$instance->strrpos($haystack, $needle, $offset); 189 } 190 191 /** 192 * Match a regexp. 193 * 194 * @param string $pattern 195 * @param string $subject 196 * @param array &$matches 197 * @param int $flags 198 * @param int $offset 199 * 200 * @return int 201 */ 202 public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) // phpcs:ignore 203 { 204 return self::$instance->preg_match($pattern, $subject, $matches, $flags, $offset); 205 } 206 207 /** 208 * Match a regexp globally. 209 * 210 * @param string $pattern 211 * @param string $subject 212 * @param array &$matches 213 * @param int $flags 214 * @param int $offset 215 * 216 * @return int 217 */ 218 public static function preg_match_all($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) // phpcs:ignore 219 { 220 return self::$instance->preg_match_all($pattern, $subject, $matches, $flags, $offset); 221 } 222 223 /** 224 * Split string by a regexp. 225 * 226 * @param string $pattern 227 * @param string $subject 228 * @param int $limit 229 * @param int $flags 230 * 231 * @return array 232 */ 233 public static function preg_split($pattern, $subject, $limit = -1, $flags = 0) // phpcs:ignore 234 { 235 return self::$instance->preg_split($pattern, $subject, $limit, $flags); 236 } 237 238 /** 239 * Search and replace by a regexp using a callback. 240 * 241 * @param string $pattern 242 * @param callable $callback 243 * @param string|array $subject 244 * @param int $limit 245 * @param int &$count 246 * 247 * @return array|string 248 */ 249 public static function preg_replace_callback($pattern, $callback, $subject, $limit = -1, &$count = 0) 250 { 251 return self::$instance->preg_replace_callback($pattern, $callback, $subject, $limit, $count); 252 } 253 254 /** 255 * Search and replace by a regexp. 256 * 257 * @param string|array $pattern 258 * @param string|array $replacement 259 * @param string|array $subject 260 * @param int $limit 261 * @param int &$count 262 * 263 * @return array|string|null 264 */ 265 public static function preg_replace($pattern, $replacement, $subject, $limit = -1, &$count = 0) 266 { 267 return self::$instance->preg_replace($pattern, $replacement, $subject, $limit, $count); 268 } 269 270 /** 271 * Check if the string is a unicode string. 272 * 273 * @param string $str String 274 * 275 * @return string|boolean 276 */ 277 public static function isUTF8($str) 278 { 279 return StringBasic::isUTF8($str); 280 } 281 282 /** 283 * Convert special chars to html entities. 284 * 285 * @param string $string The input string. 286 * @param int $quoteStyle Quote style 287 * @param string $charset Character set, UTF-8 by default 288 * @param bool $doubleEncode If set to false, no encoding of existing entities 289 * 290 * @return string 291 */ 292 public static function htmlspecialchars($string, $quoteStyle = ENT_HTML5, $charset = 'utf-8', $doubleEncode = false) 293 { 294 return htmlspecialchars( 295 $string, 296 $quoteStyle, 297 $charset, 298 $doubleEncode 299 ); 300 } 301 302 /** 303 * Convert all applicable characters to HTML entities. 304 * 305 * @param string $string The input string. 306 * @param int $quoteStyle Quote style 307 * @param string $charset Character set, UTF-8 by default 308 * @param bool $doubleEncode If set to false, no encoding of existing entities 309 * 310 * @return string 311 */ 312 public static function htmlentities($string, $quoteStyle = ENT_HTML5, $charset = 'utf-8', $doubleEncode = true) 313 { 314 return htmlentities( 315 $string, 316 $quoteStyle, 317 $charset, 318 $doubleEncode 319 ); 320 } 321} 322