bb_encoded_utf8_cut

The BuddyPress bb encoded utf8 cut function.

Description

bb_encoded_utf8_cut( $encoded, (int) $length = 0 ); 

Parameters (2)

0. $encoded
The encoded.
1. $length — Optional. (int)
The length.

Usage

  1. if ( !function_exists( 'bb_encoded_utf8_cut' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-formatting.php'; 
  3.  
  4. // The encoded. 
  5. $encoded = null; 
  6.  
  7. // The length. 
  8. $length = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_encoded_utf8_cut($encoded, $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_encoded_utf8_cut( $encoded, $length = 0 ) { 
  2. if ( $length < 1 ) 
  3. return $encoded; 
  4.  
  5. $r = ''; 
  6. $values = preg_split( '/(%[0-9a-f]{2})/i', $encoded, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );; 
  7.  
  8. for ($i = 0; $i < count( $values ); $i += $num_octets ) { 
  9. $num_octets = 1; 
  10. if ( '%' != $values[$i][0] ) { 
  11. $r .= $values[$i]; 
  12. if ( $length && strlen($r) > $length ) 
  13. return substr($r, 0, $length); 
  14. } else { 
  15. $value = hexdec(substr($values[$i], 1)); 
  16.  
  17. if ( 1 == $num_octets ) 
  18. $num_octets = ( 224 > $value ) ? 2 : 3; 
  19.  
  20. if ( $length && ( strlen($r) + $num_octets * 3 ) > $length ) 
  21. return $r; 
  22.  
  23. $r .= $values[$i] . $values[$i + 1]; 
  24. if ( 3 == $num_octets ) 
  25. $r .= $values[$i + 2]; 
  26.  
  27. return $r;