bb_utf8_cut

The BuddyPress bb utf8 cut function.

Description

bb_utf8_cut( $utf8_string, (int) $length = 0 ); 

Parameters (2)

0. $utf8_string
The utf8 string.
1. $length — Optional. (int)
The length.

Usage

  1. if ( !function_exists( 'bb_utf8_cut' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-formatting.php'; 
  3.  
  4. // The utf8 string. 
  5. $utf8_string = null; 
  6.  
  7. // The length. 
  8. $length = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_utf8_cut($utf8_string, $length); 
  12.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-formatting.php  
  1. function bb_utf8_cut( $utf8_string, $length = 0 ) { 
  2. if ( $length < 1 ) 
  3. return $utf8_string; 
  4.  
  5. $unicode = ''; 
  6. $chars = array(); 
  7. $num_octets = 1; 
  8.  
  9. for ($i = 0; $i < strlen( $utf8_string ); $i++ ) { 
  10.  
  11. $value = ord( $utf8_string[ $i ] ); 
  12.  
  13. if ( 128 > $value ) { 
  14. if ( strlen($unicode) + 1 > $length ) 
  15. break;  
  16. $unicode .= $utf8_string[ $i ]; 
  17. } else { 
  18. if ( count( $chars ) == 0 ) 
  19. $num_octets = ( 224 > $value ) ? 2 : 3; 
  20.  
  21. $chars[] = $utf8_string[ $i ]; 
  22. if ( strlen($unicode) + $num_octets > $length ) 
  23. break; 
  24. if ( count( $chars ) == $num_octets ) { 
  25. $unicode .= join('', $chars); 
  26. $chars = array(); 
  27. $num_octets = 1; 
  28.  
  29. return $unicode;