mb_substr

Compat function to mimic mb_substr().

Description

(string) mb_substr( (string) $str, (int) $start, (constant) $length = null, (null) $encoding = null ); 

Returns (string)

Extracted substring.

Parameters (4)

0. $str (string)
The string to extract the substring from.
1. $start (int)
Position to being extraction from in $str..
2. $length — Optional. (constant) => null
Maximum number of characters to extract from $str.. Default null.
3. $encoding — Optional. (null) => null
Character encoding to use. Default null.

Usage

  1. if ( !function_exists( 'mb_substr' ) ) { 
  2. require_once ABSPATH . WPINC . '/compat.php'; 
  3.  
  4. // The string to extract the substring from. 
  5. $str = ''; 
  6.  
  7. // Position to being extraction from in `$str`. 
  8. $start = -1; 
  9.  
  10. // Optional. Maximum number of characters to extract from `$str`. 
  11. // Default null. 
  12. $length = null; 
  13.  
  14. // Optional. Character encoding to use. Default null. 
  15. $encoding = null; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = mb_substr($str, $start, $length, $encoding); 
  19.  

Defined (2)

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

/wp-includes/compat.php  
  1. function mb_substr( $str, $start, $length = null, $encoding = null ) { 
  2. return _mb_substr( $str, $start, $length, $encoding ); 
  1. function _mb_substr( $str, $start, $length = null, $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 
  7. * charset just use built-in substr(). 
  8. */ 
  9. if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) { 
  10. return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length ); 
  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. $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); 
  16. return implode( '', $chars ); 
  17.  
  18. $regex = '/( 
  19. [\x00-\x7F] # single-byte sequences 0xxxxxxx 
  20. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx 
  21. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 
  22. | [\xE1-\xEC][\x80-\xBF]{2} 
  23. | \xED[\x80-\x9F][\x80-\xBF] 
  24. | [\xEE-\xEF][\x80-\xBF]{2} 
  25. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 
  26. | [\xF1-\xF3][\x80-\xBF]{3} 
  27. | \xF4[\x80-\x8F][\x80-\xBF]{2} 
  28. )/x'; 
  29.  
  30. // Start with 1 element instead of 0 since the first thing we do is pop. 
  31. $chars = array( '' ); 
  32. do { 
  33. // We had some string left over from the last round, but we counted it in that last round. 
  34. array_pop( $chars ); 
  35.  
  36. /** 
  37. * Split by UTF-8 character, limit to 1000 characters (last array element will contain 
  38. * the rest of the string). 
  39. */ 
  40. $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); 
  41.  
  42. $chars = array_merge( $chars, $pieces ); 
  43.  
  44. // If there's anything left over, repeat the loop. 
  45. } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); 
  46.  
  47. return join( '', array_slice( $chars, $start, $length ) );