wp_check_invalid_utf8

Checks for invalid UTF8 in a string.

Description

(string) wp_check_invalid_utf8( (string) $string, (bool) $strip = false ); 

Returns (string)

The checked text.

Parameters (2)

0. $string (string)
The text which is to be checked.
1. $strip — Optional. (bool) => false
Whether to attempt to strip out invalid UTF8. Default is false.

Usage

  1. if ( !function_exists( 'wp_check_invalid_utf8' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The text which is to be checked. 
  5. $string = ''; 
  6.  
  7. // Optional. Whether to attempt to strip out invalid UTF8. Default is false. 
  8. $strip = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_check_invalid_utf8($string, $strip); 
  12.  

Defined (1)

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

/wp-includes/formatting.php  
  1. function wp_check_invalid_utf8( $string, $strip = false ) { 
  2. $string = (string) $string; 
  3.  
  4. if ( 0 === strlen( $string ) ) { 
  5. return ''; 
  6.  
  7. // Store the site charset as a static to avoid multiple calls to get_option() 
  8. static $is_utf8 = null; 
  9. if ( ! isset( $is_utf8 ) ) { 
  10. $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ); 
  11. if ( ! $is_utf8 ) { 
  12. return $string; 
  13.  
  14. // Check for support for utf8 in the installed PCRE library once and store the result in a static 
  15. static $utf8_pcre = null; 
  16. if ( ! isset( $utf8_pcre ) ) { 
  17. $utf8_pcre = @preg_match( '/^./u', 'a' ); 
  18. // We can't demand utf8 in the PCRE installation, so just return the string in those cases 
  19. if ( !$utf8_pcre ) { 
  20. return $string; 
  21.  
  22. // preg_match fails when it encounters invalid UTF8 in $string 
  23. if ( 1 === @preg_match( '/^./us', $string ) ) { 
  24. return $string; 
  25.  
  26. // Attempt to strip the bad chars if requested (not recommended) 
  27. if ( $strip && function_exists( 'iconv' ) ) { 
  28. return iconv( 'utf-8', 'utf-8', $string ); 
  29.  
  30. return '';