@@ -153,13 +153,45 @@ function menu( $items, $default = null, $title = 'Choose an item' ) {
153153}
154154
155155/**
156- * An encoding-safe way of getting string length.
156+ * Attempts an encoding-safe way of getting string length. If mb_string extensions aren't
157+ * installed, falls back to basic strlen if no encoding is present
157158 *
158159 * @param string The string to check
159160 * @return int Numeric value that represents the string's length
160161 */
161162function safe_strlen ( $ str ) {
162- return mb_strlen ( $ str , mb_detect_encoding ( $ str ) );
163+ if ( function_exists ( 'mb_strlen ' ) ) {
164+ $ length = mb_strlen ( $ str , mb_detect_encoding ( $ str ) );
165+ } else {
166+ // iconv will return PHP notice if non-ascii characters are present in input string
167+ $ str = iconv ( 'ASCII ' , 'ASCII ' , $ str );
168+
169+ $ length = strlen ( $ str );
170+ }
171+
172+ return $ length ;
173+ }
174+
175+ /**
176+ * Attempts an encoding-safe way of getting a substring. If mb_string extensions aren't
177+ * installed, falls back to ascii substring if no encoding is present
178+ *
179+ * @param string $str The input string
180+ * @param int $start The starting position of the substring
181+ * @param boolean $length Maximum length of the substring
182+ * @return string Substring of string specified by start and length parameters
183+ */
184+ function safe_substr ( $ str , $ start , $ length = false ) {
185+ if ( function_exists ( 'mb_substr ' ) ) {
186+ $ substr = mb_substr ( $ str , $ start , $ length , mb_detect_encoding ( $ str ) );
187+ } else {
188+ // iconv will return PHP notice if non-ascii characters are present in input string
189+ $ str = iconv ( 'ASCII ' , 'ASCII ' , $ str );
190+
191+ $ substr = substr ( $ str , $ start , $ length );
192+ }
193+
194+ return $ substr ;
163195}
164196
165197/**
0 commit comments