o2system /
framework
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | // ------------------------------------------------------------------------ |
||||
| 12 | |||||
| 13 | if ( ! function_exists('str_echo')) { |
||||
| 14 | /** |
||||
| 15 | * str_echo |
||||
| 16 | * |
||||
| 17 | * Output one or more strings with string validation whether the string is empty or not, |
||||
| 18 | * and add additional prefix and suffix string with self-defined glue string. |
||||
| 19 | * |
||||
| 20 | * @param mixed $string String to be echo. |
||||
| 21 | * @param string $prefix Add a prefix string. |
||||
| 22 | * @param string $suffix Add a suffix string. |
||||
| 23 | * |
||||
| 24 | * @return string |
||||
| 25 | */ |
||||
| 26 | function str_echo($string, $prefix = null, $suffix = null, $glue = '') |
||||
| 27 | { |
||||
| 28 | if ( ! empty($string) OR $string !== '') { |
||||
| 29 | return implode($glue, array_filter([ |
||||
| 30 | $prefix, |
||||
| 31 | $string, |
||||
| 32 | $suffix, |
||||
| 33 | ])); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | return ''; |
||||
| 37 | } |
||||
| 38 | } |
||||
| 39 | |||||
| 40 | // ------------------------------------------------------------------------ |
||||
| 41 | |||||
| 42 | if ( ! function_exists('str_email')) { |
||||
| 43 | /** |
||||
| 44 | * str_email |
||||
| 45 | * |
||||
| 46 | * Simple save display email address. |
||||
| 47 | * |
||||
| 48 | * @param string $string Email address. |
||||
| 49 | * |
||||
| 50 | * @return string |
||||
| 51 | */ |
||||
| 52 | function str_email($string) |
||||
| 53 | { |
||||
| 54 | if ( ! empty($string) or $string != '') { |
||||
| 55 | return str_replace( |
||||
| 56 | [ |
||||
| 57 | '.', |
||||
| 58 | '@', |
||||
| 59 | ], |
||||
| 60 | [ |
||||
| 61 | ' [dot] ', |
||||
| 62 | ' [at] ', |
||||
| 63 | ], |
||||
| 64 | trim($string) |
||||
| 65 | ); |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | return $string; |
||||
| 69 | } |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | // ------------------------------------------------------------------------ |
||||
| 73 | |||||
| 74 | if ( ! function_exists('str_alphanumeric')) { |
||||
| 75 | /** |
||||
| 76 | * str_alphanumeric |
||||
| 77 | * |
||||
| 78 | * Remove non alpha-numeric characters. |
||||
| 79 | * |
||||
| 80 | * @param string |
||||
| 81 | * @param integer number of repeats |
||||
| 82 | * |
||||
| 83 | * @return string |
||||
| 84 | */ |
||||
| 85 | function str_alphanumeric($string) |
||||
| 86 | { |
||||
| 87 | if ( ! empty($string) or $string != '') { |
||||
| 88 | $string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string); |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | return trim($string); |
||||
| 92 | } |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | // ------------------------------------------------------------------------ |
||||
| 96 | |||||
| 97 | if ( ! function_exists('str_numeric')) { |
||||
| 98 | /** |
||||
| 99 | * str_numeric |
||||
| 100 | * |
||||
| 101 | * Remove non-numeric characters. |
||||
| 102 | * |
||||
| 103 | * @access public |
||||
| 104 | * |
||||
| 105 | * @param string |
||||
| 106 | * @param integer number of repeats |
||||
| 107 | * |
||||
| 108 | * @return string |
||||
| 109 | */ |
||||
| 110 | function str_numeric($string) |
||||
| 111 | { |
||||
| 112 | if ( ! empty($string) or $string != '') { |
||||
| 113 | $string = preg_replace("/[^0-9\s]/", "", $string); |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | return trim($string); |
||||
| 117 | } |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | // ------------------------------------------------------------------------ |
||||
| 121 | |||||
| 122 | if ( ! function_exists('str_truncate')) { |
||||
| 123 | /** |
||||
| 124 | * str_truncate |
||||
| 125 | * |
||||
| 126 | * Truncates a string to a certain length. |
||||
| 127 | * |
||||
| 128 | * @param string $string |
||||
| 129 | * @param int $limit |
||||
| 130 | * @param string $ending |
||||
| 131 | * |
||||
| 132 | * @return string |
||||
| 133 | */ |
||||
| 134 | function str_truncate($string, $limit = 25, $ending = '') |
||||
| 135 | { |
||||
| 136 | if (strlen($string) > $limit) { |
||||
| 137 | $string = strip_tags($string); |
||||
| 138 | $string = substr($string, 0, $limit); |
||||
| 139 | $string = substr($string, 0, -(strlen(strrchr($string, ' ')))); |
||||
| 140 | $string = $string . $ending; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | return $string; |
||||
| 144 | } |
||||
| 145 | } |
||||
| 146 | |||||
| 147 | // ------------------------------------------------------------------------ |
||||
| 148 | |||||
| 149 | if ( ! function_exists('str_shorten')) { |
||||
| 150 | /** |
||||
| 151 | * str_shorten |
||||
| 152 | * |
||||
| 153 | * Shorten a string with a limit, |
||||
| 154 | * if a string is too long will be shorten it in the middle. |
||||
| 155 | * |
||||
| 156 | * @param string $string |
||||
| 157 | * @param int $limit |
||||
| 158 | * |
||||
| 159 | * @return string |
||||
| 160 | */ |
||||
| 161 | function str_shorten($string, $limit = 25) |
||||
| 162 | { |
||||
| 163 | if (strlen($string) > $limit) { |
||||
| 164 | $prefix = substr($string, 0, ($limit / 2)); |
||||
| 165 | $suffix = substr($string, -($limit / 2)); |
||||
| 166 | $string = $prefix . ' ... ' . $suffix; |
||||
| 167 | } |
||||
| 168 | |||||
| 169 | return $string; |
||||
| 170 | } |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | // ------------------------------------------------------------------------ |
||||
| 174 | |||||
| 175 | if ( ! function_exists('str_obfuscate')) { |
||||
| 176 | /** |
||||
| 177 | * str_obfuscate |
||||
| 178 | * |
||||
| 179 | * Scrambles the source of a string. |
||||
| 180 | * |
||||
| 181 | * @param string $string |
||||
| 182 | * |
||||
| 183 | * @return string |
||||
| 184 | */ |
||||
| 185 | function str_obfuscate($string) |
||||
| 186 | { |
||||
| 187 | $length = strlen($string); |
||||
| 188 | $scrambled = ''; |
||||
| 189 | for ($i = 0; $i < $length; ++$i) { |
||||
| 190 | $scrambled .= '&#' . ord(substr($string, $i, 1)) . ';'; |
||||
| 191 | } |
||||
| 192 | |||||
| 193 | return $scrambled; |
||||
| 194 | } |
||||
| 195 | } |
||||
| 196 | |||||
| 197 | // ------------------------------------------------------------------------ |
||||
| 198 | |||||
| 199 | if ( ! function_exists('str_symbol_to_entities')) { |
||||
| 200 | |||||
| 201 | /** |
||||
| 202 | * str_symbol_to_entities |
||||
| 203 | * |
||||
| 204 | * Converts high-character symbols into their respective html entities. |
||||
| 205 | * |
||||
| 206 | * @return string |
||||
| 207 | * |
||||
| 208 | * @param string $string |
||||
| 209 | */ |
||||
| 210 | function str_symbol_to_entities($string) |
||||
| 211 | { |
||||
| 212 | static $symbols = [ |
||||
| 213 | '‚', |
||||
| 214 | 'ƒ', |
||||
| 215 | '"', |
||||
| 216 | '…', |
||||
| 217 | '†', |
||||
| 218 | '‡', |
||||
| 219 | 'ˆ', |
||||
| 220 | '‰', |
||||
| 221 | 'Š', |
||||
| 222 | '‹', |
||||
| 223 | 'Œ', |
||||
| 224 | "'", |
||||
| 225 | "'", |
||||
| 226 | '"', |
||||
| 227 | '"', |
||||
| 228 | '•', |
||||
| 229 | '–', |
||||
| 230 | '—', |
||||
| 231 | '˜', |
||||
| 232 | '™', |
||||
| 233 | 'š', |
||||
| 234 | '›', |
||||
| 235 | 'œ', |
||||
| 236 | 'Ÿ', |
||||
| 237 | '€', |
||||
| 238 | 'Æ', |
||||
| 239 | 'Á', |
||||
| 240 | 'Â', |
||||
| 241 | 'À', |
||||
| 242 | 'Å', |
||||
| 243 | 'Ã', |
||||
| 244 | 'Ä', |
||||
| 245 | 'Ç', |
||||
| 246 | 'Ð', |
||||
| 247 | 'É', |
||||
| 248 | 'Ê', |
||||
| 249 | 'È', |
||||
| 250 | 'Ë', |
||||
| 251 | 'Í', |
||||
| 252 | 'Î', |
||||
| 253 | 'Ì', |
||||
| 254 | 'Ï', |
||||
| 255 | 'Ñ', |
||||
| 256 | 'Ó', |
||||
| 257 | 'Ô', |
||||
| 258 | 'Ò', |
||||
| 259 | 'Ø', |
||||
| 260 | 'Õ', |
||||
| 261 | 'Ö', |
||||
| 262 | 'Þ', |
||||
| 263 | 'Ú', |
||||
| 264 | 'Û', |
||||
| 265 | 'Ù', |
||||
| 266 | 'Ü', |
||||
| 267 | 'Ý', |
||||
| 268 | 'á', |
||||
| 269 | 'â', |
||||
| 270 | 'æ', |
||||
| 271 | 'à', |
||||
| 272 | 'å', |
||||
| 273 | 'ã', |
||||
| 274 | 'ä', |
||||
| 275 | 'ç', |
||||
| 276 | 'é', |
||||
| 277 | 'ê', |
||||
| 278 | 'è', |
||||
| 279 | 'ð', |
||||
| 280 | 'ë', |
||||
| 281 | 'í', |
||||
| 282 | 'î', |
||||
| 283 | 'ì', |
||||
| 284 | 'ï', |
||||
| 285 | 'ñ', |
||||
| 286 | 'ó', |
||||
| 287 | 'ô', |
||||
| 288 | 'ò', |
||||
| 289 | 'ø', |
||||
| 290 | 'õ', |
||||
| 291 | 'ö', |
||||
| 292 | 'ß', |
||||
| 293 | 'þ', |
||||
| 294 | 'ú', |
||||
| 295 | 'û', |
||||
| 296 | 'ù', |
||||
| 297 | 'ü', |
||||
| 298 | 'ý', |
||||
| 299 | 'ÿ', |
||||
| 300 | '¡', |
||||
| 301 | '£', |
||||
| 302 | '¤', |
||||
| 303 | '¥', |
||||
| 304 | '¦', |
||||
| 305 | '§', |
||||
| 306 | '¨', |
||||
| 307 | '©', |
||||
| 308 | 'ª', |
||||
| 309 | '«', |
||||
| 310 | '¬', |
||||
| 311 | '', |
||||
| 312 | '®', |
||||
| 313 | '¯', |
||||
| 314 | '°', |
||||
| 315 | '±', |
||||
| 316 | '²', |
||||
| 317 | '³', |
||||
| 318 | '´', |
||||
| 319 | 'µ', |
||||
| 320 | '¶', |
||||
| 321 | '·', |
||||
| 322 | '¸', |
||||
| 323 | '¹', |
||||
| 324 | 'º', |
||||
| 325 | '»', |
||||
| 326 | '¼', |
||||
| 327 | '½', |
||||
| 328 | '¾', |
||||
| 329 | '¿', |
||||
| 330 | '×', |
||||
| 331 | '÷', |
||||
| 332 | '¢', |
||||
| 333 | '…', |
||||
| 334 | 'µ', |
||||
| 335 | ]; |
||||
| 336 | static $entities = [ |
||||
| 337 | '‚', |
||||
| 338 | 'ƒ', |
||||
| 339 | '„', |
||||
| 340 | '…', |
||||
| 341 | '†', |
||||
| 342 | '‡', |
||||
| 343 | 'ˆ', |
||||
| 344 | '‰', |
||||
| 345 | 'Š', |
||||
| 346 | '‹', |
||||
| 347 | 'Œ', |
||||
| 348 | '‘', |
||||
| 349 | '’', |
||||
| 350 | '“', |
||||
| 351 | '”', |
||||
| 352 | '•', |
||||
| 353 | '–', |
||||
| 354 | '—', |
||||
| 355 | '˜', |
||||
| 356 | '™', |
||||
| 357 | 'š', |
||||
| 358 | '›', |
||||
| 359 | 'œ', |
||||
| 360 | 'Ÿ', |
||||
| 361 | '€', |
||||
| 362 | 'æ', |
||||
| 363 | 'á', |
||||
| 364 | 'â', |
||||
| 365 | 'à', |
||||
| 366 | 'å', |
||||
| 367 | 'ã', |
||||
| 368 | 'ä', |
||||
| 369 | 'ç', |
||||
| 370 | 'ð', |
||||
| 371 | 'é', |
||||
| 372 | 'ê', |
||||
| 373 | 'è', |
||||
| 374 | 'ë', |
||||
| 375 | 'í', |
||||
| 376 | 'î', |
||||
| 377 | 'ì', |
||||
| 378 | 'ï', |
||||
| 379 | 'ñ', |
||||
| 380 | 'ó', |
||||
| 381 | 'ô', |
||||
| 382 | 'ò', |
||||
| 383 | 'ø', |
||||
| 384 | 'õ', |
||||
| 385 | 'ö', |
||||
| 386 | 'þ', |
||||
| 387 | 'ú', |
||||
| 388 | 'û', |
||||
| 389 | 'ù', |
||||
| 390 | 'ü', |
||||
| 391 | 'ý', |
||||
| 392 | 'á', |
||||
| 393 | 'â', |
||||
| 394 | 'æ', |
||||
| 395 | 'à', |
||||
| 396 | 'å', |
||||
| 397 | 'ã', |
||||
| 398 | 'ä', |
||||
| 399 | 'ç', |
||||
| 400 | 'é', |
||||
| 401 | 'ê', |
||||
| 402 | 'è', |
||||
| 403 | 'ð', |
||||
| 404 | 'ë', |
||||
| 405 | 'í', |
||||
| 406 | 'î', |
||||
| 407 | 'ì', |
||||
| 408 | 'ï', |
||||
| 409 | 'ñ', |
||||
| 410 | 'ó', |
||||
| 411 | 'ô', |
||||
| 412 | 'ò', |
||||
| 413 | 'ø', |
||||
| 414 | 'õ', |
||||
| 415 | 'ö', |
||||
| 416 | 'ß', |
||||
| 417 | 'þ', |
||||
| 418 | 'ú', |
||||
| 419 | 'û', |
||||
| 420 | 'ù', |
||||
| 421 | 'ü', |
||||
| 422 | 'ý', |
||||
| 423 | 'ÿ', |
||||
| 424 | '¡', |
||||
| 425 | '£', |
||||
| 426 | '¤', |
||||
| 427 | '¥', |
||||
| 428 | '¦', |
||||
| 429 | '§', |
||||
| 430 | '¨', |
||||
| 431 | '©', |
||||
| 432 | 'ª', |
||||
| 433 | '«', |
||||
| 434 | '¬', |
||||
| 435 | '­', |
||||
| 436 | '®', |
||||
| 437 | '¯', |
||||
| 438 | '°', |
||||
| 439 | '±', |
||||
| 440 | '²', |
||||
| 441 | '³', |
||||
| 442 | '´', |
||||
| 443 | 'µ', |
||||
| 444 | '¶', |
||||
| 445 | '·', |
||||
| 446 | '¸', |
||||
| 447 | '¹', |
||||
| 448 | 'º', |
||||
| 449 | '»', |
||||
| 450 | '¼', |
||||
| 451 | '½', |
||||
| 452 | '¾', |
||||
| 453 | '¿', |
||||
| 454 | '×', |
||||
| 455 | '÷', |
||||
| 456 | '¢', |
||||
| 457 | '...', |
||||
| 458 | 'µ', |
||||
| 459 | ]; |
||||
| 460 | |||||
| 461 | return str_replace($symbols, $entities, $string); |
||||
| 462 | } |
||||
| 463 | } |
||||
| 464 | |||||
| 465 | // ------------------------------------------------------------------------ |
||||
| 466 | |||||
| 467 | if ( ! function_exists('str_strip_slashes')) { |
||||
| 468 | /** |
||||
| 469 | * str_strip_slashes |
||||
| 470 | * |
||||
| 471 | * Removes slashes contained in a string or in an array. |
||||
| 472 | * |
||||
| 473 | * @param mixed string or array |
||||
| 474 | * |
||||
| 475 | * @return mixed string or array |
||||
| 476 | */ |
||||
| 477 | function str_strip_slashes($string) |
||||
| 478 | { |
||||
| 479 | if ( ! is_array($string)) { |
||||
| 480 | return stripslashes($string); |
||||
| 481 | } |
||||
| 482 | |||||
| 483 | foreach ($string as $key => $val) { |
||||
| 484 | $string[ $key ] = str_strip_slashes($val); |
||||
| 485 | } |
||||
| 486 | |||||
| 487 | return $string; |
||||
| 488 | } |
||||
| 489 | } |
||||
| 490 | |||||
| 491 | // ------------------------------------------------------------------------ |
||||
| 492 | |||||
| 493 | if ( ! function_exists('str_quote_strip')) { |
||||
| 494 | /** |
||||
| 495 | * str_quote_strip |
||||
| 496 | * |
||||
| 497 | * Removes single and double quotes from a string. |
||||
| 498 | * |
||||
| 499 | * @param string |
||||
| 500 | * |
||||
| 501 | * @return string |
||||
| 502 | */ |
||||
| 503 | function str_quote_strip($str) |
||||
| 504 | { |
||||
| 505 | return str_replace(['"', "'"], '', $str); |
||||
| 506 | } |
||||
| 507 | } |
||||
| 508 | |||||
| 509 | // ------------------------------------------------------------------------ |
||||
| 510 | |||||
| 511 | if ( ! function_exists('str_quote_to_entities')) { |
||||
| 512 | /** |
||||
| 513 | * str_quote_to_entities |
||||
| 514 | * |
||||
| 515 | * Converts single and double quotes to entities. |
||||
| 516 | * |
||||
| 517 | * @param string |
||||
| 518 | * |
||||
| 519 | * @return string |
||||
| 520 | */ |
||||
| 521 | function str_quote_to_entities($string) |
||||
| 522 | { |
||||
| 523 | return str_replace(["\'", "\"", "'", '"'], ["'", """, "'", """], $string); |
||||
| 524 | } |
||||
| 525 | } |
||||
| 526 | |||||
| 527 | // ------------------------------------------------------------------------ |
||||
| 528 | |||||
| 529 | |||||
| 530 | if ( ! function_exists('str_filter_char')) { |
||||
| 531 | /** |
||||
| 532 | * str_filter_char |
||||
| 533 | * |
||||
| 534 | * Reduces multiple instances of a particular character. |
||||
| 535 | * |
||||
| 536 | * @example |
||||
| 537 | * Fred, Bill,, Joe, Jimmy |
||||
| 538 | * |
||||
| 539 | * @result |
||||
| 540 | * Fred, Bill, Joe, Jimmy |
||||
| 541 | * |
||||
| 542 | * @param string $str The string to be filtered. |
||||
| 543 | * @param string $character The character you wish to reduce. |
||||
| 544 | * @param bool $trim TRUE/FALSE - whether to trim the character from the beginning/end. |
||||
| 545 | * |
||||
| 546 | * @return string |
||||
| 547 | */ |
||||
| 548 | function str_filter_char($str, $character = ',', $trim = false) |
||||
| 549 | { |
||||
| 550 | $str = preg_replace('#' . preg_quote($character, '#') . '{2,}#', $character, $str); |
||||
| 551 | |||||
| 552 | return ($trim === true) ? trim($str, $character) : $str; |
||||
| 553 | } |
||||
| 554 | } |
||||
| 555 | |||||
| 556 | // ------------------------------------------------------------------------ |
||||
| 557 | |||||
| 558 | if ( ! function_exists('str_rand')) { |
||||
| 559 | /** |
||||
| 560 | * str_rand |
||||
| 561 | * |
||||
| 562 | * Create a random string, very useful for generating passwords or hashes. |
||||
| 563 | * |
||||
| 564 | * @param string $type Type of random string. |
||||
| 565 | * basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1 |
||||
| 566 | * @param int $length Number of characters |
||||
| 567 | * |
||||
| 568 | * @return string |
||||
| 569 | */ |
||||
| 570 | function str_rand($type = 'alnum', $length = 8) |
||||
| 571 | { |
||||
| 572 | switch ($type) { |
||||
| 573 | case 'basic': |
||||
| 574 | return mt_rand(); |
||||
| 575 | case 'alnum': |
||||
| 576 | case 'numeric': |
||||
| 577 | case 'nozero': |
||||
| 578 | case 'alpha': |
||||
| 579 | switch ($type) { |
||||
| 580 | case 'alpha': |
||||
| 581 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||||
| 582 | break; |
||||
| 583 | case 'alnum': |
||||
| 584 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||||
| 585 | break; |
||||
| 586 | case 'numeric': |
||||
| 587 | $pool = '0123456789'; |
||||
| 588 | break; |
||||
| 589 | case 'nozero': |
||||
| 590 | $pool = '123456789'; |
||||
| 591 | break; |
||||
| 592 | } |
||||
| 593 | |||||
| 594 | return substr(str_shuffle(str_repeat($pool, ceil($length / strlen($pool)))), 0, $length); |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
ceil($length / strlen($pool)) of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 595 | case 'unique': // todo: remove in 3.1+ |
||||
| 596 | case 'md5': |
||||
| 597 | return md5(uniqid(mt_rand())); |
||||
| 598 | case 'encrypt': // todo: remove in 3.1+ |
||||
| 599 | case 'sha1': |
||||
| 600 | return sha1(uniqid(mt_rand(), true)); |
||||
| 601 | } |
||||
| 602 | } |
||||
| 603 | } |
||||
| 604 | |||||
| 605 | // ------------------------------------------------------------------------ |
||||
| 606 | |||||
| 607 | if ( ! function_exists('str_inc')) { |
||||
| 608 | /** |
||||
| 609 | * str_inc |
||||
| 610 | * |
||||
| 611 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc. |
||||
| 612 | * |
||||
| 613 | * @param string $string The string to be increased. |
||||
| 614 | * @param string $separator What should the duplicate number be appended with. |
||||
| 615 | * @param string $first Which number should be used for the first dupe increment |
||||
| 616 | * |
||||
| 617 | * @return string |
||||
| 618 | */ |
||||
| 619 | function str_inc($string, $separator = '_', $first = 1) |
||||
| 620 | { |
||||
| 621 | preg_match('/(.+)' . $separator . '([0-9]+)$/', $string, $match); |
||||
| 622 | |||||
| 623 | return isset($match[ 2 ]) ? $match[ 1 ] . $separator . ($match[ 2 ] + 1) : $string . $separator . $first; |
||||
| 624 | } |
||||
| 625 | } |
||||
| 626 | |||||
| 627 | // ------------------------------------------------------------------------ |
||||
| 628 | |||||
| 629 | if ( ! function_exists('str_alt')) { |
||||
| 630 | /** |
||||
| 631 | * str_alt |
||||
| 632 | * |
||||
| 633 | * Allows strings to be alternated. See docs... |
||||
| 634 | * |
||||
| 635 | * @param string $param as many parameters as needed. |
||||
| 636 | * |
||||
| 637 | * @return string |
||||
| 638 | */ |
||||
| 639 | function str_alt() |
||||
| 640 | { |
||||
| 641 | static $i; |
||||
| 642 | |||||
| 643 | if (func_num_args() === 0) { |
||||
| 644 | $i = 0; |
||||
| 645 | |||||
| 646 | return ''; |
||||
| 647 | } |
||||
| 648 | |||||
| 649 | $args = func_get_args(); |
||||
| 650 | |||||
| 651 | return $args[ ($i++ % count($args)) ]; |
||||
| 652 | } |
||||
| 653 | } |
||||
| 654 | |||||
| 655 | if ( ! function_exists('str_char_to_ascii')) { |
||||
| 656 | /** |
||||
| 657 | * str_char_to_ascii |
||||
| 658 | * |
||||
| 659 | * Convert accented foreign characters to ASCII. |
||||
| 660 | * |
||||
| 661 | * @param string $string The string to be converted. |
||||
| 662 | * |
||||
| 663 | * @return string |
||||
| 664 | */ |
||||
| 665 | function str_chars_to_ascii($string) |
||||
| 666 | { |
||||
| 667 | static $array_from, $array_to; |
||||
| 668 | |||||
| 669 | if ( ! is_array($array_from)) { |
||||
| 670 | $foreign_characters = [ |
||||
| 671 | '/ä|æ|ǽ/' => 'ae', |
||||
| 672 | '/ö|œ/' => 'oe', |
||||
| 673 | '/ü/' => 'ue', |
||||
| 674 | '/Ä/' => 'Ae', |
||||
| 675 | '/Ü/' => 'Ue', |
||||
| 676 | '/Ö/' => 'Oe', |
||||
| 677 | '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|Α|Ά|Ả|Ạ|Ầ|Ẫ|Ẩ|Ậ|Ằ|Ắ|Ẵ|Ẳ|Ặ|А/' => 'A', |
||||
| 678 | '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|α|ά|ả|ạ|ầ|ấ|ẫ|ẩ|ậ|ằ|ắ|ẵ|ẳ|ặ|а/' => 'a', |
||||
| 679 | '/Б/' => 'B', |
||||
| 680 | '/б/' => 'b', |
||||
| 681 | '/Ç|Ć|Ĉ|Ċ|Č/' => 'C', |
||||
| 682 | '/ç|ć|ĉ|ċ|č/' => 'c', |
||||
| 683 | '/Д/' => 'D', |
||||
| 684 | '/д/' => 'd', |
||||
| 685 | '/Ð|Ď|Đ|Δ/' => 'Dj', |
||||
| 686 | '/ð|ď|đ|δ/' => 'dj', |
||||
| 687 | '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Ε|Έ|Ẽ|Ẻ|Ẹ|Ề|Ế|Ễ|Ể|Ệ|Е|Э/' => 'E', |
||||
| 688 | '/è|é|ê|ë|ē|ĕ|ė|ę|ě|έ|ε|ẽ|ẻ|ẹ|ề|ế|ễ|ể|ệ|е|э/' => 'e', |
||||
| 689 | '/Ф/' => 'F', |
||||
| 690 | '/ф/' => 'f', |
||||
| 691 | '/Ĝ|Ğ|Ġ|Ģ|Γ|Г|Ґ/' => 'G', |
||||
| 692 | '/ĝ|ğ|ġ|ģ|γ|г|ґ/' => 'g', |
||||
| 693 | '/Ĥ|Ħ/' => 'H', |
||||
| 694 | '/ĥ|ħ/' => 'h', |
||||
| 695 | '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|Η|Ή|Ί|Ι|Ϊ|Ỉ|Ị|И|Ы/' => 'I', |
||||
| 696 | '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|η|ή|ί|ι|ϊ|ỉ|ị|и|ы|ї/' => 'i', |
||||
| 697 | '/Ĵ/' => 'J', |
||||
| 698 | '/ĵ/' => 'j', |
||||
| 699 | '/Ķ|Κ|К/' => 'K', |
||||
| 700 | '/ķ|κ|к/' => 'k', |
||||
| 701 | '/Ĺ|Ļ|Ľ|Ŀ|Ł|Λ|Л/' => 'L', |
||||
| 702 | '/ĺ|ļ|ľ|ŀ|ł|λ|л/' => 'l', |
||||
| 703 | '/М/' => 'M', |
||||
| 704 | '/м/' => 'm', |
||||
| 705 | '/Ñ|Ń|Ņ|Ň|Ν|Н/' => 'N', |
||||
| 706 | '/ñ|ń|ņ|ň|ʼn|ν|н/' => 'n', |
||||
| 707 | '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ|Ο|Ό|Ω|Ώ|Ỏ|Ọ|Ồ|Ố|Ỗ|Ổ|Ộ|Ờ|Ớ|Ỡ|Ở|Ợ|О/' => 'O', |
||||
| 708 | '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|ο|ό|ω|ώ|ỏ|ọ|ồ|ố|ỗ|ổ|ộ|ờ|ớ|ỡ|ở|ợ|о/' => 'o', |
||||
| 709 | '/П/' => 'P', |
||||
| 710 | '/п/' => 'p', |
||||
| 711 | '/Ŕ|Ŗ|Ř|Ρ|Р/' => 'R', |
||||
| 712 | '/ŕ|ŗ|ř|ρ|р/' => 'r', |
||||
| 713 | '/Ś|Ŝ|Ş|Ș|Š|Σ|С/' => 'S', |
||||
| 714 | '/ś|ŝ|ş|ș|š|ſ|σ|ς|с/' => 's', |
||||
| 715 | '/Ț|Ţ|Ť|Ŧ|τ|Т/' => 'T', |
||||
| 716 | '/ț|ţ|ť|ŧ|т/' => 't', |
||||
| 717 | '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ|Ũ|Ủ|Ụ|Ừ|Ứ|Ữ|Ử|Ự|У/' => 'U', |
||||
| 718 | '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ|υ|ύ|ϋ|ủ|ụ|ừ|ứ|ữ|ử|ự|у/' => 'u', |
||||
| 719 | '/Ý|Ÿ|Ŷ|Υ|Ύ|Ϋ|Ỳ|Ỹ|Ỷ|Ỵ|Й/' => 'Y', |
||||
| 720 | '/ý|ÿ|ŷ|ỳ|ỹ|ỷ|ỵ|й/' => 'y', |
||||
| 721 | '/В/' => 'V', |
||||
| 722 | '/в/' => 'v', |
||||
| 723 | '/Ŵ/' => 'W', |
||||
| 724 | '/ŵ/' => 'w', |
||||
| 725 | '/Ź|Ż|Ž|Ζ|З/' => 'Z', |
||||
| 726 | '/ź|ż|ž|ζ|з/' => 'z', |
||||
| 727 | '/Æ|Ǽ/' => 'AE', |
||||
| 728 | '/ß/' => 'ss', |
||||
| 729 | '/IJ/' => 'IJ', |
||||
| 730 | '/ij/' => 'ij', |
||||
| 731 | '/Œ/' => 'OE', |
||||
| 732 | '/ƒ/' => 'f', |
||||
| 733 | '/ξ/' => 'ks', |
||||
| 734 | '/π/' => 'p', |
||||
| 735 | '/β/' => 'v', |
||||
| 736 | '/μ/' => 'm', |
||||
| 737 | '/ψ/' => 'ps', |
||||
| 738 | '/Ё/' => 'Yo', |
||||
| 739 | '/ё/' => 'yo', |
||||
| 740 | '/Є/' => 'Ye', |
||||
| 741 | '/є/' => 'ye', |
||||
| 742 | '/Ї/' => 'Yi', |
||||
| 743 | '/Ж/' => 'Zh', |
||||
| 744 | '/ж/' => 'zh', |
||||
| 745 | '/Х/' => 'Kh', |
||||
| 746 | '/х/' => 'kh', |
||||
| 747 | '/Ц/' => 'Ts', |
||||
| 748 | '/ц/' => 'ts', |
||||
| 749 | '/Ч/' => 'Ch', |
||||
| 750 | '/ч/' => 'ch', |
||||
| 751 | '/Ш/' => 'Sh', |
||||
| 752 | '/ш/' => 'sh', |
||||
| 753 | '/Щ/' => 'Shch', |
||||
| 754 | '/щ/' => 'shch', |
||||
| 755 | '/Ъ|ъ|Ь|ь/' => '', |
||||
| 756 | '/Ю/' => 'Yu', |
||||
| 757 | '/ю/' => 'yu', |
||||
| 758 | '/Я/' => 'Ya', |
||||
| 759 | '/я/' => 'ya', |
||||
| 760 | ]; |
||||
| 761 | |||||
| 762 | $array_from = array_keys($foreign_characters); |
||||
| 763 | $array_to = array_values($foreign_characters); |
||||
| 764 | } |
||||
| 765 | |||||
| 766 | return preg_replace($array_from, $array_to, $string); |
||||
| 767 | } |
||||
| 768 | } |
||||
| 769 | |||||
| 770 | // ------------------------------------------------------------------------ |
||||
| 771 | |||||
| 772 | |||||
| 773 | if ( ! function_exists('str_entities_to_ascii')) { |
||||
| 774 | /** |
||||
| 775 | * str_entities_to_ascii |
||||
| 776 | * |
||||
| 777 | * Converts character entities back to ASCII. |
||||
| 778 | * |
||||
| 779 | * @param string $string The string to be converted. |
||||
| 780 | * @param bool $all TRUE/FALSE - whether convert all entities or not. |
||||
| 781 | * |
||||
| 782 | * @return string |
||||
| 783 | */ |
||||
| 784 | function str_entities_to_ascii($string, $all = true) |
||||
| 785 | { |
||||
| 786 | if (preg_match_all('/\&#(\d+)\;/', $string, $matches)) { |
||||
| 787 | for ($i = 0, $s = count($matches[ 0 ]); $i < $s; $i++) { |
||||
| 788 | $digits = $matches[ 1 ][ $i ]; |
||||
| 789 | $out = ''; |
||||
| 790 | |||||
| 791 | if ($digits < 128) { |
||||
| 792 | $out .= chr($digits); |
||||
| 793 | |||||
| 794 | } elseif ($digits < 2048) { |
||||
| 795 | $out .= chr(192 + (($digits - ($digits % 64)) / 64)) . chr(128 + ($digits % 64)); |
||||
| 796 | } else { |
||||
| 797 | $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)) |
||||
| 798 | . chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)) |
||||
| 799 | . chr(128 + ($digits % 64)); |
||||
| 800 | } |
||||
| 801 | |||||
| 802 | $string = str_replace($matches[ 0 ][ $i ], $out, $string); |
||||
| 803 | } |
||||
| 804 | } |
||||
| 805 | |||||
| 806 | if ($all) { |
||||
| 807 | return str_replace( |
||||
| 808 | ['&', '<', '>', '"', ''', '-'], |
||||
| 809 | ['&', '<', '>', '"', "'", '-'], |
||||
| 810 | $string |
||||
| 811 | ); |
||||
| 812 | } |
||||
| 813 | |||||
| 814 | return $string; |
||||
| 815 | } |
||||
| 816 | } |
||||
| 817 | |||||
| 818 | // ------------------------------------------------------------------------ |
||||
| 819 | |||||
| 820 | |||||
| 821 | if ( ! function_exists('str_ascii_to_entities')) { |
||||
| 822 | /** |
||||
| 823 | * str_ascii_to_entities |
||||
| 824 | * |
||||
| 825 | * Converts high ASCII text and MS Word special characters to character entities. |
||||
| 826 | * |
||||
| 827 | * @param string $string The string to be converted. |
||||
| 828 | * |
||||
| 829 | * @return string |
||||
| 830 | */ |
||||
| 831 | function str_ascii_to_entities($string) |
||||
| 832 | { |
||||
| 833 | $out = ''; |
||||
| 834 | for ($i = 0, $s = strlen($string) - 1, $count = 1, $temp = []; $i <= $s; $i++) { |
||||
| 835 | $ordinal = ord($string[ $i ]); |
||||
| 836 | |||||
| 837 | if ($ordinal < 128) { |
||||
| 838 | /* |
||||
| 839 | If the $temp array has a value but we have moved on, then it seems only |
||||
| 840 | fair that we output that entity and restart $temp before continuing. -Paul |
||||
| 841 | */ |
||||
| 842 | if (count($temp) === 1) { |
||||
| 843 | $out .= '&#' . array_shift($temp) . ';'; |
||||
| 844 | $count = 1; |
||||
| 845 | } |
||||
| 846 | |||||
| 847 | $out .= $string[ $i ]; |
||||
| 848 | } else { |
||||
| 849 | if (count($temp) === 0) { |
||||
| 850 | $count = ($ordinal < 224) ? 2 : 3; |
||||
| 851 | } |
||||
| 852 | |||||
| 853 | $temp[] = $ordinal; |
||||
| 854 | |||||
| 855 | if (count($temp) === $count) { |
||||
| 856 | $number = ($count === 3) |
||||
| 857 | ? (($temp[ 0 ] % 16) * 4096) + (($temp[ 1 ] % 64) * 64) + ($temp[ 2 ] % 64) |
||||
| 858 | : (($temp[ 0 ] % 32) * 64) + ($temp[ 1 ] % 64); |
||||
| 859 | |||||
| 860 | $out .= '&#' . $number . ';'; |
||||
| 861 | $count = 1; |
||||
| 862 | $temp = []; |
||||
| 863 | } // If this is the last iteration, just output whatever we have |
||||
| 864 | elseif ($i === $s) { |
||||
| 865 | $out .= '&#' . implode(';', $temp) . ';'; |
||||
| 866 | } |
||||
| 867 | } |
||||
| 868 | } |
||||
| 869 | |||||
| 870 | return $out; |
||||
| 871 | } |
||||
| 872 | } |