|
| 1 | +<?php |
| 2 | + |
| 3 | +if (!function_exists('mb_str_split')) { |
| 4 | + function mb_str_split($string, $split_length = 1, $encoding = null) |
| 5 | + { |
| 6 | + if ( |
| 7 | + null !== $string |
| 8 | + && !is_scalar($string) |
| 9 | + && !(is_object($string) |
| 10 | + && method_exists($string, '__toString')) |
| 11 | + ) { |
| 12 | + trigger_error( |
| 13 | + 'mb_str_split(): expects parameter 1 to be string, ' . gettype($string) . ' given', |
| 14 | + E_USER_WARNING |
| 15 | + ); |
| 16 | + return null; |
| 17 | + } |
| 18 | + if (null !== $split_length && !is_bool($split_length) && !is_numeric($split_length)) { |
| 19 | + trigger_error( |
| 20 | + 'mb_str_split(): expects parameter 2 to be int, ' . gettype($split_length) . ' given', |
| 21 | + E_USER_WARNING |
| 22 | + ); |
| 23 | + return null; |
| 24 | + } |
| 25 | + $split_length = (int) $split_length; |
| 26 | + if (1 > $split_length) { |
| 27 | + trigger_error('mb_str_split(): The length of each segment must be greater than zero', E_USER_WARNING); |
| 28 | + return false; |
| 29 | + } |
| 30 | + if (null === $encoding) { |
| 31 | + $encoding = mb_internal_encoding(); |
| 32 | + } else { |
| 33 | + $encoding = (string) $encoding; |
| 34 | + } |
| 35 | + |
| 36 | + if (!in_array($encoding, mb_list_encodings(), true)) { |
| 37 | + static $aliases; |
| 38 | + if ($aliases === null) { |
| 39 | + $aliases = array(); |
| 40 | + foreach (mb_list_encodings() as $encoding) { |
| 41 | + $encoding_aliases = mb_encoding_aliases($encoding); |
| 42 | + if ($encoding_aliases) { |
| 43 | + foreach ($encoding_aliases as $alias) { |
| 44 | + $aliases[] = $alias; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + if (! in_array($encoding, $aliases, true)) { |
| 50 | + trigger_error('mb_str_split(): Unknown encoding "' . $encoding . '"', E_USER_WARNING); |
| 51 | + return null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + $result = array(); |
| 56 | + $length = mb_strlen($string, $encoding); |
| 57 | + for ($i = 0; $i < $length; $i += $split_length) { |
| 58 | + $result[] = mb_substr($string, $i, $split_length, $encoding); |
| 59 | + } |
| 60 | + return $result; |
| 61 | + } |
| 62 | +} |
0 commit comments