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 * @see basename() 17 * @link https://bugs.php.net/bug.php?id=37738 18 * 19 * @param string $path A path 20 * @param string $suffix If the name component ends in suffix this will also be cut off 21 * @return string 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 - It's 44 * even faster than mb_strlen. 45 * 46 * @author <chernyshevsky at hotmail dot com> 47 * @see strlen() 48 * @see utf8_decode() 49 * 50 * @param string $string 51 * @return int 52 */ 53 public static function strlen($string) 54 { 55 if (function_exists('utf8_decode')) { 56 return strlen(utf8_decode($string)); 57 } 58 59 if (UTF8_MBSTRING) { 60 return mb_strlen($string, 'UTF-8'); 61 } 62 63 if (function_exists('iconv_strlen')) { 64 return iconv_strlen($string, 'UTF-8'); 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 * @author Harry Fuecks <hfuecks@gmail.com> 76 * @author Chris Smith <chris@jalakai.co.uk> 77 * 78 * @param string $str 79 * @param int $offset number of UTF-8 characters offset (from left) 80 * @param int $length (optional) length in UTF-8 characters from offset 81 * @return string 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 142 if (!isset($strlen)) $strlen = self::strlen($str); // see notes 143 if ($offset > $strlen) return ''; // another trivial case 144 145 if ($length > 0) { 146 147 $length = min($strlen - $offset, $length); // reduce any length that would go past the end of the string 148 149 $Lx = (int)($length / 65535); 150 $Ly = $length % 65535; 151 152 // +ve length requires ... a captured group of length characters 153 if ($Lx) $length_pattern = '(?:.{65535}){' . $Lx . '}'; 154 $length_pattern = '(' . $length_pattern . '.{' . $Ly . '})'; 155 156 } else if ($length < 0) { 157 158 if ($length < ($offset - $strlen)) return ''; 159 160 $Lx = (int)((-$length) / 65535); 161 $Ly = (-$length) % 65535; 162 163 // -ve length requires ... capture everything except a group of -length characters 164 // anchored at the tail-end of the string 165 if ($Lx) $length_pattern = '(?:.{65535}){' . $Lx . '}'; 166 $length_pattern = '(.*)(?:' . $length_pattern . '.{' . $Ly . '})$'; 167 } 168 } 169 170 if (!preg_match('#' . $offset_pattern . $length_pattern . '#us', $str, $match)) return ''; 171 return $match[1]; 172 } 173 174 /** 175 * Unicode aware replacement for substr_replace() 176 * 177 * @author Andreas Gohr <andi@splitbrain.org> 178 * @see substr_replace() 179 * 180 * @param string $string input string 181 * @param string $replacement the replacement 182 * @param int $start the replacing will begin at the start'th offset into string. 183 * @param int $length If given and is positive, it represents the length of the portion of string which is 184 * to be replaced. If length is zero then this function will have the effect of inserting 185 * replacement into string at the given start offset. 186 * @return string 187 */ 188 public static function substr_replace($string, $replacement, $start, $length = 0) 189 { 190 $ret = ''; 191 if ($start > 0) $ret .= self::substr($string, 0, $start); 192 $ret .= $replacement; 193 $ret .= self::substr($string, $start + $length); 194 return $ret; 195 } 196 197 /** 198 * Unicode aware replacement for ltrim() 199 * 200 * @author Andreas Gohr <andi@splitbrain.org> 201 * @see ltrim() 202 * 203 * @param string $str 204 * @param string $charlist 205 * @return string 206 */ 207 public static function ltrim($str, $charlist = '') 208 { 209 if ($charlist === '') return ltrim($str); 210 211 //quote charlist for use in a characterclass 212 $charlist = preg_replace('!([\\\\\\-\\]\\[/])!', '\\\${1}', $charlist); 213 214 return preg_replace('/^[' . $charlist . ']+/u', '', $str); 215 } 216 217 /** 218 * Unicode aware replacement for rtrim() 219 * 220 * @author Andreas Gohr <andi@splitbrain.org> 221 * @see rtrim() 222 * 223 * @param string $str 224 * @param string $charlist 225 * @return string 226 */ 227 public static function rtrim($str, $charlist = '') 228 { 229 if ($charlist === '') return rtrim($str); 230 231 //quote charlist for use in a characterclass 232 $charlist = preg_replace('!([\\\\\\-\\]\\[/])!', '\\\${1}', $charlist); 233 234 return preg_replace('/[' . $charlist . ']+$/u', '', $str); 235 } 236 237 /** 238 * Unicode aware replacement for trim() 239 * 240 * @author Andreas Gohr <andi@splitbrain.org> 241 * @see trim() 242 * 243 * @param string $str 244 * @param string $charlist 245 * @return string 246 */ 247 public static function trim($str, $charlist = '') 248 { 249 if ($charlist === '') return trim($str); 250 251 return self::ltrim(self::rtrim($str, $charlist), $charlist); 252 } 253 254 /** 255 * This is a unicode aware replacement for strtolower() 256 * 257 * Uses mb_string extension if available 258 * 259 * @author Leo Feyer <leo@typolight.org> 260 * @see strtolower() 261 * @see utf8_strtoupper() 262 * 263 * @param string $string 264 * @return string 265 */ 266 public static function strtolower($string) 267 { 268 if (UTF8_MBSTRING) { 269 if (class_exists('Normalizer', $autoload = false)) { 270 return \Normalizer::normalize(mb_strtolower($string, 'utf-8')); 271 } 272 return (mb_strtolower($string, 'utf-8')); 273 } 274 return strtr($string, Table::upperCaseToLowerCase()); 275 } 276 277 /** 278 * This is a unicode aware replacement for strtoupper() 279 * 280 * Uses mb_string extension if available 281 * 282 * @author Leo Feyer <leo@typolight.org> 283 * @see strtoupper() 284 * @see utf8_strtoupper() 285 * 286 * @param string $string 287 * @return string 288 */ 289 public static function strtoupper($string) 290 { 291 if (UTF8_MBSTRING) return mb_strtoupper($string, 'utf-8'); 292 293 return strtr($string, Table::lowerCaseToUpperCase()); 294 } 295 296 297 /** 298 * UTF-8 aware alternative to ucfirst 299 * Make a string's first character uppercase 300 * 301 * @author Harry Fuecks 302 * 303 * @param string $str 304 * @return string with first character as upper case (if applicable) 305 */ 306 public static function ucfirst($str) 307 { 308 switch (self::strlen($str)) { 309 case 0: 310 return ''; 311 case 1: 312 return self::strtoupper($str); 313 default: 314 preg_match('/^(.{1})(.*)$/us', $str, $matches); 315 return self::strtoupper($matches[1]) . $matches[2]; 316 } 317 } 318 319 /** 320 * UTF-8 aware alternative to ucwords 321 * Uppercase the first character of each word in a string 322 * 323 * @author Harry Fuecks 324 * @see http://php.net/ucwords 325 * 326 * @param string $str 327 * @return string with first char of each word uppercase 328 */ 329 public static function ucwords($str) 330 { 331 // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches; 332 // form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns 333 // This corresponds to the definition of a "word" defined at http://php.net/ucwords 334 $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u'; 335 336 return preg_replace_callback( 337 $pattern, 338 function ($matches) { 339 $leadingws = $matches[2]; 340 $ucfirst = self::strtoupper($matches[3]); 341 $ucword = self::substr_replace(ltrim($matches[0]), $ucfirst, 0, 1); 342 return $leadingws . $ucword; 343 }, 344 $str 345 ); 346 } 347 348 /** 349 * This is an Unicode aware replacement for strpos 350 * 351 * @author Leo Feyer <leo@typolight.org> 352 * @see strpos() 353 * 354 * @param string $haystack 355 * @param string $needle 356 * @param integer $offset 357 * @return integer 358 */ 359 public static function strpos($haystack, $needle, $offset = 0) 360 { 361 $comp = 0; 362 $length = null; 363 364 while ($length === null || $length < $offset) { 365 $pos = strpos($haystack, $needle, $offset + $comp); 366 367 if ($pos === false) 368 return false; 369 370 $length = self::strlen(substr($haystack, 0, $pos)); 371 372 if ($length < $offset) 373 $comp = $pos - $length; 374 } 375 376 return $length; 377 } 378 379 380} 381