_wp_json_convert_string

Convert a string to UTF-8, so that it can be safely encoded to JSON.

Description

(string) _wp_json_convert_string( (string) $string ); 

Returns (string)

The checked string.

Parameters (1)

0. $string (string)
The string which is to be converted.

Usage

  1. if ( !function_exists( '_wp_json_convert_string' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // The string which is to be converted. 
  5. $string = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = _wp_json_convert_string($string); 
  9.  

Defined (1)

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

/wp-includes/functions.php  
  1. function _wp_json_convert_string( $string ) { 
  2. static $use_mb = null; 
  3. if ( is_null( $use_mb ) ) { 
  4. $use_mb = function_exists( 'mb_convert_encoding' ); 
  5.  
  6. if ( $use_mb ) { 
  7. $encoding = mb_detect_encoding( $string, mb_detect_order(), true ); 
  8. if ( $encoding ) { 
  9. return mb_convert_encoding( $string, 'UTF-8', $encoding ); 
  10. } else { 
  11. return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' ); 
  12. } else { 
  13. return wp_check_invalid_utf8( $string, true );