|
21 | 21 |
|
22 | 22 | // region |
23 | 23 |
|
| 24 | +/** |
| 25 | + * Polyfill for mb_strimwidth. |
| 26 | + */ |
| 27 | +if ( ! function_exists( 'mb_strimwidth' ) ) { |
| 28 | + /** |
| 29 | + * Polyfill for mb_strimwidth. |
| 30 | + * |
| 31 | + * @param string $source The string to trim. |
| 32 | + * @param integer $start The starting position. |
| 33 | + * @param integer $width The width to trim to. |
| 34 | + * @param string $trim_marker The marker to append if the string is trimmed. |
| 35 | + * @param string $encoding The character encoding. |
| 36 | + * |
| 37 | + * @return string |
| 38 | + */ |
| 39 | + function mb_strimwidth( $source, $start, $width, $trim_marker = '', $encoding = 'UTF-8' ) { |
| 40 | + // Fallback using mb_substr if available |
| 41 | + if ( function_exists( 'mb_substr' ) ) { |
| 42 | + $substr = mb_substr( $source, $start, $width, $encoding ); |
| 43 | + if ( mb_strlen( $source, $encoding ) > $width ) { |
| 44 | + return $substr . $trim_marker; |
| 45 | + } |
| 46 | + return $substr; |
| 47 | + } else { |
| 48 | + // Rough fallback using substr (not multibyte-safe!) |
| 49 | + $substr = substr( $source, $start, $width ); |
| 50 | + if ( strlen( $source ) > $width ) { |
| 51 | + return $substr . $trim_marker; |
| 52 | + } |
| 53 | + return $substr; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Polyfill for mb_strlen. |
| 60 | + */ |
| 61 | +if ( ! function_exists( 'mb_strlen' ) ) { |
| 62 | + /** |
| 63 | + * Polyfill for mb_strlen. |
| 64 | + * |
| 65 | + * @param string $source The string to measure. |
| 66 | + * @param string $encoding The character encoding. |
| 67 | + * |
| 68 | + * @return integer |
| 69 | + */ |
| 70 | + function mb_strlen( $source, $encoding = 'UTF-8' ) { |
| 71 | + // Use preg_match_all to count UTF-8 characters |
| 72 | + if ( 'UTF-8' === $encoding ) { |
| 73 | + return preg_match_all( '/./u', $source, $matches ); |
| 74 | + } |
| 75 | + |
| 76 | + // Fallback: use strlen (not multibyte-safe!) |
| 77 | + return strlen( $source ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
24 | 81 | /** |
25 | 82 | * Returns the plugin's main class instance. |
26 | 83 | * |
|
0 commit comments