wpcf7_count_code_units

Returns the number of code units in a string.

Description

(int|bool) wpcf7_count_code_units( $string ); 

Returns (int|bool)

The number of code units, or false if mb_convert_encoding is not available.

Parameters (1)

0. $string
The string.

Usage

  1. if ( !function_exists( 'wpcf7_count_code_units' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/includes/functions.php'; 
  3.  
  4. // The string. 
  5. $string = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wpcf7_count_code_units($string); 
  9.  

Defined (1)

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

/includes/functions.php  
  1. function wpcf7_count_code_units( $string ) { 
  2. static $use_mb = null; 
  3.  
  4. if ( is_null( $use_mb ) ) { 
  5. $use_mb = function_exists( 'mb_convert_encoding' ); 
  6.  
  7. if ( ! $use_mb ) { 
  8. return false; 
  9.  
  10. $string = (string) $string; 
  11.  
  12. $encoding = mb_detect_encoding( $string, mb_detect_order(), true ); 
  13.  
  14. if ( $encoding ) { 
  15. $string = mb_convert_encoding( $string, 'UTF-16', $encoding ); 
  16. } else { 
  17. $string = mb_convert_encoding( $string, 'UTF-16', 'UTF-8' ); 
  18.  
  19. $byte_count = mb_strlen( $string, '8bit' ); 
  20.  
  21. return floor( $byte_count / 2 );