Make WordPress Core

Ticket #30633: 30633.2.diff

File 30633.2.diff, 2.5 KB (added by SergeyBiryukov, 8 years ago)

Similar to the current mb_substr() implementation, with some cleanup

  • src/wp-includes/compat.php

     
    1313        }
    1414}
    1515
    16 if ( !function_exists('mb_substr') ):
    17         function mb_substr( $str, $start, $length=null, $encoding=null ) {
    18                 return _mb_substr($str, $start, $length, $encoding);
     16if ( ! function_exists( 'mb_substr' ) ) :
     17        function mb_substr( $str, $start, $length = null, $encoding = null ) {
     18                return _mb_substr( $str, $start, $length, $encoding );
    1919        }
    2020endif;
    2121
    22 function _mb_substr( $str, $start, $length=null, $encoding=null ) {
    23         // the solution below, works only for utf-8, so in case of a different
    24         // charset, just use built-in substr
     22function _mb_substr( $str, $start, $length = null, $encoding = null ) {
     23        // The solution below works only for UTF-8,
     24        // so in case of a different charset just use built-in substr()
    2525        $charset = get_option( 'blog_charset' );
    26         if ( !in_array( $charset, array('utf8', 'utf-8', 'UTF8', 'UTF-8') ) ) {
    27                 return is_null( $length )? substr( $str, $start ) : substr( $str, $start, $length);
     26        if ( ! in_array( $charset, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
     27                return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
    2828        }
    29         // use the regex unicode support to separate the UTF-8 characters into an array
     29        // Use the regex unicode support to separate the UTF-8 characters into an array
    3030        preg_match_all( '/./us', $str, $match );
    31         $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
     31        $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
    3232        return implode( '', $chars );
    3333}
    3434
     35if ( ! function_exists( 'mb_strlen' ) ) :
     36        function mb_strlen( $str, $encoding = null ) {
     37                return _mb_strlen( $str, $encoding );
     38        }
     39endif;
     40
     41function _mb_strlen( $str, $encoding = null ) {
     42        // The solution below works only for UTF-8,
     43        // so in case of a different charset just use built-in substr()
     44        $charset = get_option( 'blog_charset' );
     45        if ( ! in_array( $charset, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
     46                return strlen( $str );
     47        }
     48        // Use the regex unicode support to separate the UTF-8 characters into an array
     49        preg_match_all( '/./us', $str, $match );
     50        return count( $match[0] );
     51}
     52
    3553if ( !function_exists('hash_hmac') ):
    3654function hash_hmac($algo, $data, $key, $raw_output = false) {
    3755        return _hash_hmac($algo, $data, $key, $raw_output);