template-tags
6 days ago
multibyte.php
6 days ago
query.php
6 days ago
url.php
6 days ago
utils.php
6 days ago
multibyte.php
194 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Formatting function wrappers to allow use of |
| 4 | * multibyte string functions where they are available |
| 5 | * as using base functions on a multibyte string will porovide incorrect results |
| 6 | * |
| 7 | * @since 4.7.18 |
| 8 | */ |
| 9 | |
| 10 | if ( ! function_exists( 'tribe_detect_encoding' ) ) { |
| 11 | /** |
| 12 | * Detects multibyte encoding if the function is available, returns false if not |
| 13 | * @since 4.7.18 |
| 14 | * |
| 15 | * @param string $string the string to detect encoding of |
| 16 | * @return string|bool the mb encoding of the string |
| 17 | * false if the function is not available or the encoding cannot be determined |
| 18 | */ |
| 19 | function tribe_detect_encoding( $string ) { |
| 20 | return function_exists( 'mb_detect_encoding' ) ? mb_detect_encoding( $string ) : false; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | if ( ! function_exists( 'tribe_maybe_urldecode' ) ) { |
| 25 | /** |
| 26 | * Detects urlencoded strings if the function is available, and converts them. |
| 27 | * Returns false if not able to detect |
| 28 | * @since 4.7.18 |
| 29 | * |
| 30 | * @param string $string the string to detect encoding of |
| 31 | * @return string|bool the urldecoded string |
| 32 | * the original string if the function is not available or the encoding cannot be determined |
| 33 | */ |
| 34 | function tribe_maybe_urldecode( $string ) { |
| 35 | $encoding = function_exists( 'mb_detect_encoding' ) ? mb_detect_encoding( $string ) : $string; |
| 36 | |
| 37 | if ( 'ASCII' === $encoding ) { |
| 38 | return urldecode( $string ); |
| 39 | } |
| 40 | |
| 41 | return $string; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if ( ! function_exists( 'tribe_strlen' ) ) { |
| 46 | /** |
| 47 | * Get the length of a string, uses mb_strlen when available |
| 48 | * @since 4.7.18 |
| 49 | * |
| 50 | * @param string $string the string to get the length of |
| 51 | * @return int string length |
| 52 | */ |
| 53 | function tribe_strlen( $string ) { |
| 54 | if ( function_exists( 'mb_strlen' ) ) { |
| 55 | $encoding = tribe_detect_encoding( $string ); |
| 56 | $string = tribe_maybe_urldecode( $string ); |
| 57 | // we test for encoding and pass it if we get it |
| 58 | if ( $encoding ) { |
| 59 | return mb_strlen( $string, $encoding ); |
| 60 | } |
| 61 | |
| 62 | return mb_strlen( $string ); |
| 63 | } |
| 64 | |
| 65 | return strlen( $string ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if ( ! function_exists( 'tribe_substr' ) ) { |
| 70 | /** |
| 71 | * Get a substring, using multibyte functions if available |
| 72 | * @since 4.7.18 |
| 73 | * |
| 74 | * @param string $string string to crop |
| 75 | * @param int $start start position |
| 76 | * @param int $length (optional) substring length |
| 77 | * @return int substring |
| 78 | */ |
| 79 | function tribe_substr( $string, $start = 0, $length = null ) { |
| 80 | // Handle a passed $length |
| 81 | if ( ! empty( $length ) ) { |
| 82 | return function_exists( 'mb_substr' ) ? mb_substr( $string, $start, $length ) : substr( $string, $start, $length ); |
| 83 | } |
| 84 | |
| 85 | return function_exists( 'mb_substr' ) ? mb_substr( $string, $start ) : substr( $string, $start ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if ( ! function_exists( 'tribe_strtoupper' ) ) { |
| 90 | /** |
| 91 | * Convert string to uppercase, using multibyte functions if available |
| 92 | * @since 4.7.18 |
| 93 | * |
| 94 | * @param string $string string to convert |
| 95 | * @return string converted string |
| 96 | */ |
| 97 | function tribe_strtoupper( $string ) { |
| 98 | // If it's a number, don't try to convert it |
| 99 | if ( is_numeric( $string ) ) { |
| 100 | return $string; |
| 101 | } |
| 102 | |
| 103 | if ( function_exists( 'mb_strtoupper' ) ) { |
| 104 | $encoding = tribe_detect_encoding( $string ); |
| 105 | $string = tribe_maybe_urldecode( $string ); |
| 106 | // we test for encoding and pass it if we get it |
| 107 | if ( $encoding ) { |
| 108 | return mb_strtoupper( $string, $encoding ); |
| 109 | } |
| 110 | |
| 111 | return mb_strtoupper( $string ); |
| 112 | } |
| 113 | |
| 114 | return strtoupper( $string ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if ( ! function_exists( 'tribe_strtolower' ) ) { |
| 119 | /** |
| 120 | * Convert string to lowercase, using multibyte functions if available |
| 121 | * @since 4.7.18 |
| 122 | * |
| 123 | * @param string $string string to convert |
| 124 | * @return string converted string |
| 125 | */ |
| 126 | function tribe_strtolower( $string ) { |
| 127 | // If it's a number, don't try to convert it |
| 128 | if ( is_numeric( $string ) ) { |
| 129 | return $string; |
| 130 | } |
| 131 | |
| 132 | if ( function_exists( 'mb_strtolower' ) ) { |
| 133 | $encoding = tribe_detect_encoding( $string ); |
| 134 | $string = tribe_maybe_urldecode( $string ); |
| 135 | // we test for encoding and pass it if we get it |
| 136 | if ( $encoding ) { |
| 137 | return mb_strtolower( $string, $encoding ); |
| 138 | } |
| 139 | |
| 140 | return mb_strtolower( $string ); |
| 141 | } |
| 142 | |
| 143 | return strtolower( $string ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( ! function_exists( 'tribe_uc_first_letter' ) ) { |
| 148 | function tribe_uc_first_letter( $string ) { |
| 149 | if ( is_numeric( $string ) ) { |
| 150 | return $string; |
| 151 | } |
| 152 | |
| 153 | $first_char = tribe_substr( $string, 0, 1 ); |
| 154 | $letter = tribe_strtoupper( $first_char ); |
| 155 | // fallback in case it gets garbled |
| 156 | if ( '?' === $letter ) { |
| 157 | $letter = $first_char; |
| 158 | } |
| 159 | |
| 160 | return $letter; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if ( ! function_exists( 'tribe_strpos' ) ) { |
| 165 | /** |
| 166 | * Find the numeric position of the first occurrence of needle in the haystack string using multibyte function if available. |
| 167 | * |
| 168 | * @since 4.9.19 |
| 169 | * |
| 170 | * @param string $haystack The string to search in. |
| 171 | * @param string $needle The string to find in haystack. |
| 172 | * @param int $offset The search offset. If it is not specified, 0 is used. A negative offset counts from the end of the string. |
| 173 | * |
| 174 | * @return int|false The numeric position of the first occurrence of needle in the haystack string. If needle is not found, it returns false. |
| 175 | * |
| 176 | * @see strpos The fallback function used if mb_strpos does not exist. |
| 177 | * @see mb_strpos The multibyte compatible version of strpos. |
| 178 | */ |
| 179 | function tribe_strpos( $haystack, $needle, $offset = 0 ) { |
| 180 | if ( function_exists( 'mb_strpos' ) ) { |
| 181 | $encoding = tribe_detect_encoding( $haystack ); |
| 182 | |
| 183 | // Use encoding if it was detected. |
| 184 | if ( $encoding ) { |
| 185 | return mb_strpos( $haystack, $needle, $offset, $encoding ); |
| 186 | } |
| 187 | |
| 188 | return mb_strpos( $haystack, $needle, $offset ); |
| 189 | } |
| 190 | |
| 191 | return strpos( $haystack, $needle, $offset ); |
| 192 | } |
| 193 | } |
| 194 |