@@ -183,6 +183,10 @@ function safe_strlen( $str ) {
183183 * @return string Substring of string specified by start and length parameters
184184 */
185185function safe_substr ( $ str , $ start , $ length = false , $ width = false ) {
186+ // PHP 5.3 substr takes false as full length, PHP > 5.3 takes null - for compat. do strlen.
187+ if ( null === $ length || false === $ length ) {
188+ $ length = safe_strlen ( $ str );
189+ }
186190 if ( function_exists ( 'mb_substr ' ) && function_exists ( 'mb_detect_encoding ' ) ) {
187191 $ encoding = mb_detect_encoding ( $ str );
188192 if ( false !== $ width && 'UTF-8 ' === $ encoding ) {
@@ -191,11 +195,13 @@ function safe_substr( $str, $start, $length = false, $width = false ) {
191195 // Load both regexs generated from Unicode data.
192196 require __DIR__ . '/unicode/regex.php ' ;
193197 }
194- $ cnt = preg_match_all ( '/[\x00-\x7f\xc2-\xf4][^\x00-\x7f\xc2-\xf4]*/ ' , $ str , $ matches );
195- $ width = $ length ;
198+ if ( preg_match ( $ eaw_regex , $ str ) ) {
199+ $ cnt = preg_match_all ( '/[\x00-\x7f\xc2-\xf4][^\x00-\x7f\xc2-\xf4]*/ ' , $ str , $ matches );
200+ $ width = $ length ;
196201
197- for ( $ length = 0 ; $ length < $ cnt && $ width > 0 ; $ length ++ ) {
198- $ width -= preg_match ( $ eaw_regex , $ matches [0 ][ $ length ] ) ? 2 : 1 ;
202+ for ( $ length = 0 ; $ length < $ cnt && $ width > 0 ; $ length ++ ) {
203+ $ width -= preg_match ( $ eaw_regex , $ matches [0 ][ $ length ] ) ? 2 : 1 ;
204+ }
199205 }
200206 }
201207 $ substr = mb_substr ( $ str , $ start , $ length , $ encoding );
0 commit comments