mbstring_binary_safe_encoding

Set the mbstring internal encoding to a binary safe encoding when func_overload is enabled.

Description

mbstring_binary_safe_encoding( (bool) $reset = false ); 

When mbstring.func_overload is in use for multi-byte encodings, the results from strlen() and similar functions respect the utf8 characters, causing binary data to return incorrect lengths.

This function overrides the mbstring encoding to a binary-safe encoding, and resets it to the users expected encoding afterwards through the 'reset_mbstring_encoding' function.

It is safe to recursively call this function, however each 'mbstring_binary_safe_encoding()' call must be followed up with an equal number of reset_mbstring_encoding() calls.

Parameters (1)

0. $reset — Optional. (bool) => false
Whether to reset the encoding back to a previously-set encoding. Default false.

Usage

  1. if ( !function_exists( 'mbstring_binary_safe_encoding' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Optional. Whether to reset the encoding back to a previously-set encoding. 
  5. // Default false. 
  6. $reset = false; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = mbstring_binary_safe_encoding($reset); 
  10.  

Defined (1)

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

/wp-includes/functions.php  
  1. function mbstring_binary_safe_encoding( $reset = false ) { 
  2. static $encodings = array(); 
  3. static $overloaded = null; 
  4.  
  5. if ( is_null( $overloaded ) ) 
  6. $overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 ); 
  7.  
  8. if ( false === $overloaded ) 
  9. return; 
  10.  
  11. if ( ! $reset ) { 
  12. $encoding = mb_internal_encoding(); 
  13. array_push( $encodings, $encoding ); 
  14. mb_internal_encoding( 'ISO-8859-1' ); 
  15.  
  16. if ( $reset && $encodings ) { 
  17. $encoding = array_pop( $encodings ); 
  18. mb_internal_encoding( $encoding );