WordPress.org

Make WordPress Core

Changeset 32114


Ignore:
Timestamp:
04/11/2015 11:13:01 PM (4 years ago)
Author:
azaozz
Message:

Add mb_strlen() compatibility function. Works the same way as the existing mb_substr() compatibility function.
Props SergeyBiryukov. Fixes #31951.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r31188 r32114  
    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 );
     33}
     34
     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] );
    3351}
    3452
Note: See TracChangeset for help on using the changeset viewer.