1<?php 2 3namespace dokuwiki\Utf8; 4 5/** 6 * UTF-8 aware equivalents to PHP's string functions 7 */ 8class PhpString 9{ 10 11 /** 12 * A locale independent basename() implementation 13 * 14 * works around a bug in PHP's basename() implementation 15 * 16 * @param string $path A path 17 * @param string $suffix If the name component ends in suffix this will also be cut off 18 * @return string 19 * @link https://bugs.php.net/bug.php?id=37738 20 * 21 * @see basename() 22 */ 23 public static function basename($path, $suffix = '') 24 { 25 $path = trim($path, '\\/'); 26 $rpos = max(strrpos($path, '/'), strrpos($path, '\\')); 27 if ($rpos) { 28 $path = substr($path, $rpos + 1); 29 } 30 31 $suflen = strlen($suffix); 32 if ($suflen && (substr($path, -$suflen) === $suffix)) { 33 $path = substr($path, 0, -$suflen); 34 } 35 36 return $path; 37 } 38 39 /** 40 * Unicode aware replacement for strlen() 41 * 42 * utf8_decode() converts characters that are not in ISO-8859-1 43 * to '?', which, for the purpose of counting, is alright 44 * 45 * @param string $string 46 * @return int 47 * @see utf8_decode() 48 * 49 * @author <chernyshevsky at hotmail dot com> 50 * @see strlen() 51 */ 52 public static function strlen($string) 53 { 54 if (UTF8_MBSTRING) { 55 return mb_strlen($string, 'UTF-8'); 56 } 57 58 if (function_exists('iconv_strlen')) { 59 return iconv_strlen($string, 'UTF-8'); 60 } 61 62 // utf8_decode is deprecated 63 if (function_exists('utf8_decode')) { 64 return strlen(utf8_decode($string)); 65 } 66 67 return strlen($string); 68 } 69 70 /** 71 * UTF-8 aware alternative to substr 72 * 73 * Return part of a string given character offset (and optionally length) 74 * 75 * @param string $str 76 * @param int $offset number of UTF-8 characters offset (from left) 77 * @param int $length (optional) length in UTF-8 characters from offset 78 * @return string 79 * @author Harry Fuecks <hfuecks@gmail.com> 80 * @author Chris Smith <chris@jalakai.co.uk> 81 * 82 */ 83 public static function substr($str, $offset, $length = null) 84 { 85 if (UTF8_MBSTRING) { 86 if ($length === null) { 87 return mb_substr($str, $offset); 88 } 89 90 return mb_substr($str, $offset, $length); 91 } 92 93 /* 94 * Notes: 95 * 96 * no mb string support, so we'll use pcre regex's with 'u' flag 97 * pcre only supports repetitions of less than 65536, in order to accept up to MAXINT values for 98 * offset and length, we'll repeat a group of 65535 characters when needed (ok, up to MAXINT-65536) 99 * 100 * substr documentation states false can be returned in some cases (e.g. offset > string length) 101 * mb_substr never returns false, it will return an empty string instead. 102 * 103 * calculating the number of characters in the string is a relatively expensive operation, so 104 * we only carry it out when necessary. It isn't necessary for +ve offsets and no specified length 105 */ 106 107 // cast parameters to appropriate types to avoid multiple notices/warnings 108 $str = (string)$str; // generates E_NOTICE for PHP4 objects, but not PHP5 objects 109 $offset = (int)$offset; 110 if ($length !== null) $length = (int)$length; 111 112 // handle trivial cases 113 if ($length === 0) return ''; 114 if ($offset < 0 && $length < 0 && $length < $offset) return ''; 115 116 $offset_pattern = ''; 117 $length_pattern = ''; 118 119 // normalise -ve offsets (we could use a tail anchored pattern, but they are horribly slow!) 120 if ($offset < 0) { 121 $strlen = self::strlen($str); // see notes 122 $offset = $strlen + $offset; 123 if ($offset < 0) $offset = 0; 124 } 125 126 // establish a pattern for offset, a non-captured group equal in length to offset 127 if ($offset > 0) { 128 $Ox = (int)($offset / 65535); 129 $Oy = $offset % 65535; 130 131 if ($Ox) $offset_pattern = '(?:.{65535}){' . $Ox . '}'; 132 $offset_pattern = '^(?:' . $offset_pattern . '.{' . $Oy . '})'; 133 } else { 134 $offset_pattern = '^'; // offset == 0; just anchor the pattern 135 } 136 137 // establish a pattern for length 138 if ($length === null) { 139 $length_pattern = '(.*)$'; // the rest of the string 140 } else { 141 if (!isset($strlen)) $strlen = self::strlen($str); // see notes 142 if ($offset > $strlen) return ''; // another trivial case 143 144 if ($length > 0) { 145 // reduce any length that would go past the end of the string 146 $length = min($strlen - $offset, $length); 147 $Lx = (int)($length / 65535); 148 $Ly = $length % 65535; 149 // +ve length requires ... a captured group of length characters 150 if ($Lx) $length_pattern = '(?:.{65535}){' . $Lx . '}'; 151 $length_pattern = '(' . $length_pattern . '.{' . $Ly . '})'; 152 } elseif ($length < 0) { 153 if ($length < ($offset - $strlen)) return ''; 154 $Lx = (int)((-$length) / 65535); 155 $Ly = (-$length) % 65535; 156 // -ve length requires ... capture everything except a group of -length characters 157 // anchored at the tail-end of the string 158 if ($Lx) $length_pattern = '(?:.{65535}){' . $Lx . '}'; 159 $length_pattern = '(.*)(?:' . $length_pattern . '.{' . $Ly . '})$'; 160 } 161 } 162 163 if (!preg_match('#' . $offset_pattern . $length_pattern . '#us', $str, $match)) return ''; 164 return $match[1]; 165 } 166 167 // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps 168 /** 169 * Unicode aware replacement for substr_replace() 170 * 171 * @param string $string input string 172 * @param string $replacement the replacement 173 * @param int $start the replacing will begin at the start'th offset into string. 174 * @param int $length If given and is positive, it represents the length of the portion of string which is 175 * to be replaced. If length is zero then this function will have the effect of inserting 176 * replacement into string at the given start offset. 177 * @return string 178 * @see substr_replace() 179 * 180 * @author Andreas Gohr <andi@splitbrain.org> 181 */ 182 public static function substr_replace($string, $replacement, $start, $length = 0) 183 { 184 $ret = ''; 185 if ($start > 0) $ret .= self::substr($string, 0, $start); 186 $ret .= $replacement; 187 $ret .= self::substr($string, $start + $length); 188 return $ret; 189 } 190 // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps 191 192 /** 193 * Unicode aware replacement for ltrim() 194 * 195 * @param string $str 196 * @param string $charlist 197 * @return string 198 * @see ltrim() 199 * 200 * @author Andreas Gohr <andi@splitbrain.org> 201 */ 202 public static function ltrim($str, $charlist = '') 203 { 204 if ($charlist === '') return ltrim($str); 205 206 //quote charlist for use in a characterclass 207 $charlist = preg_replace('!([\\\\\\-\\]\\[/])!', '\\\${1}', $charlist); 208 209 return preg_replace('/^[' . $charlist . ']+/u', '', $str); 210 } 211 212 /** 213 * Unicode aware replacement for rtrim() 214 * 215 * @param string $str 216 * @param string $charlist 217 * @return string 218 * @see rtrim() 219 * 220 * @author Andreas Gohr <andi@splitbrain.org> 221 */ 222 public static function rtrim($str, $charlist = '') 223 { 224 if ($charlist === '') return rtrim($str); 225 226 //quote charlist for use in a characterclass 227 $charlist = preg_replace('!([\\\\\\-\\]\\[/])!', '\\\${1}', $charlist); 228 229 return preg_replace('/[' . $charlist . ']+$/u', '', $str); 230 } 231 232 /** 233 * Unicode aware replacement for trim() 234 * 235 * @param string $str 236 * @param string $charlist 237 * @return string 238 * @see trim() 239 * 240 * @author Andreas Gohr <andi@splitbrain.org> 241 */ 242 public static function trim($str, $charlist = '') 243 { 244 if ($charlist === '') return trim($str); 245 246 return self::ltrim(self::rtrim($str, $charlist), $charlist); 247 } 248 249 /** 250 * This is a unicode aware replacement for strtolower() 251 * 252 * Uses mb_string extension if available 253 * 254 * @param string $string 255 * @return string 256 * @see \dokuwiki\Utf8\PhpString::strtoupper() 257 * 258 * @author Leo Feyer <leo@typolight.org> 259 * @see strtolower() 260 */ 261 public static function strtolower($string) 262 { 263 if ($string === null) return ''; // pre-8.1 behaviour 264 if (UTF8_MBSTRING) { 265 if (class_exists('Normalizer', $autoload = false)) { 266 return \Normalizer::normalize(mb_strtolower($string, 'utf-8')); 267 } 268 return (mb_strtolower($string, 'utf-8')); 269 } 270 return strtr($string, Table::upperCaseToLowerCase()); 271 } 272 273 /** 274 * This is a unicode aware replacement for strtoupper() 275 * 276 * Uses mb_string extension if available 277 * 278 * @param string $string 279 * @return string 280 * @see \dokuwiki\Utf8\PhpString::strtoupper() 281 * 282 * @author Leo Feyer <leo@typolight.org> 283 * @see strtoupper() 284 */ 285 public static function strtoupper($string) 286 { 287 if (UTF8_MBSTRING) return mb_strtoupper($string, 'utf-8'); 288 289 return strtr($string, Table::lowerCaseToUpperCase()); 290 } 291 292 293 /** 294 * UTF-8 aware alternative to ucfirst 295 * Make a string's first character uppercase 296 * 297 * @param string $str 298 * @return string with first character as upper case (if applicable) 299 * @author Harry Fuecks 300 * 301 */ 302 public static function ucfirst($str) 303 { 304 switch (self::strlen($str)) { 305 case 0: 306 return ''; 307 case 1: 308 return self::strtoupper($str); 309 default: 310 preg_match('/^(.{1})(.*)$/us', $str, $matches); 311 return self::strtoupper($matches[1]) . $matches[2]; 312 } 313 } 314 315 /** 316 * UTF-8 aware alternative to ucwords 317 * Uppercase the first character of each word in a string 318 * 319 * @param string $str 320 * @return string with first char of each word uppercase 321 * @author Harry Fuecks 322 * @see http://php.net/ucwords 323 * 324 */ 325 public static function ucwords($str) 326 { 327 // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches; 328 // form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns 329 // This corresponds to the definition of a "word" defined at http://php.net/ucwords 330 $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u'; 331 332 return preg_replace_callback( 333 $pattern, 334 function ($matches) { 335 $leadingws = $matches[2]; 336 $ucfirst = self::strtoupper($matches[3]); 337 $ucword = self::substr_replace(ltrim($matches[0]), $ucfirst, 0, 1); 338 return $leadingws . $ucword; 339 }, 340 $str 341 ); 342 } 343 344 /** 345 * This is an Unicode aware replacement for strpos 346 * 347 * @param string $haystack 348 * @param string $needle 349 * @param integer $offset 350 * @return integer 351 * @author Leo Feyer <leo@typolight.org> 352 * @see strpos() 353 * 354 */ 355 public static function strpos($haystack, $needle, $offset = 0) 356 { 357 $comp = 0; 358 $length = null; 359 360 while ($length === null || $length < $offset) { 361 $pos = strpos($haystack, $needle, $offset + $comp); 362 363 if ($pos === false) 364 return false; 365 366 $length = self::strlen(substr($haystack, 0, $pos)); 367 368 if ($length < $offset) 369 $comp = $pos - $length; 370 } 371 372 return $length; 373 } 374} 375