mb_strlen

Compat function to mimic mb_strlen().

Description

(int) mb_strlen( (string) $str, (null) $encoding = null ); 

Returns (int)

String length of `$str`.

Parameters (2)

0. $str (string)
The string to retrieve the character length from.
1. $encoding — Optional. (null) => null
Character encoding to use. Default null.

Usage

  1. if ( !function_exists( 'mb_strlen' ) ) { 
  2. require_once ABSPATH . WPINC . '/compat.php'; 
  3.  
  4. // The string to retrieve the character length from. 
  5. $str = ''; 
  6.  
  7. // Optional. Character encoding to use. Default null. 
  8. $encoding = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = mb_strlen($str, $encoding); 
  12.  

Defined (2)

The function is defined in the following location(s).

/wp-includes/compat.php  
  1. function mb_strlen( $str, $encoding = null ) { 
  2. return _mb_strlen( $str, $encoding ); 
  1. function _mb_strlen( $str, $encoding = null ) { 
  2. if ( null === $encoding ) { 
  3. $encoding = get_option( 'blog_charset' ); 
  4.  
  5. /** 
  6. * The solution below works only for UTF-8, so in case of a different charset 
  7. * just use built-in strlen(). 
  8. */ 
  9. if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) { 
  10. return strlen( $str ); 
  11.  
  12. if ( _wp_can_use_pcre_u() ) { 
  13. // Use the regex unicode support to separate the UTF-8 characters into an array. 
  14. preg_match_all( '/./us', $str, $match ); 
  15. return count( $match[0] ); 
  16.  
  17. $regex = '/(?: 
  18. [\x00-\x7F] # single-byte sequences 0xxxxxxx 
  19. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx 
  20. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 
  21. | [\xE1-\xEC][\x80-\xBF]{2} 
  22. | \xED[\x80-\x9F][\x80-\xBF] 
  23. | [\xEE-\xEF][\x80-\xBF]{2} 
  24. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 
  25. | [\xF1-\xF3][\x80-\xBF]{3} 
  26. | \xF4[\x80-\x8F][\x80-\xBF]{2} 
  27. )/x'; 
  28.  
  29. // Start at 1 instead of 0 since the first thing we do is decrement. 
  30. $count = 1; 
  31. do { 
  32. // We had some string left over from the last round, but we counted it in that last round. 
  33. $count--; 
  34.  
  35. /** 
  36. * Split by UTF-8 character, limit to 1000 characters (last array element will contain 
  37. * the rest of the string). 
  38. */ 
  39. $pieces = preg_split( $regex, $str, 1000 ); 
  40.  
  41. // Increment. 
  42. $count += count( $pieces ); 
  43.  
  44. // If there's anything left over, repeat the loop. 
  45. } while ( $str = array_pop( $pieces ) ); 
  46.  
  47. // Fencepost: preg_split() always returns one extra item in the array. 
  48. return --$count;