Fix character encoding detection in CSV exporter for PHP 8.1
In PHP 8.1 the behavior of mb_detect_encoding has changed from "return the first suitable encoding from the list" to "return the most probable encoding from the list". The algorithm used for "most probable" often fails, though, and detects valid UTF-8 strings as if they were ISO-8859-1. The fix is to use mb_check_encoding instead. Additionally, if conversion is needed, mb_convert_encoding is now used since utf8_encode is deprecated.
This commit is contained in:
parent
5c4746cefc
commit
6c2781eedf
|
@ -398,8 +398,10 @@ abstract class WC_CSV_Exporter {
|
|||
$use_mb = function_exists( 'mb_convert_encoding' );
|
||||
|
||||
if ( $use_mb ) {
|
||||
$encoding = mb_detect_encoding( $data, 'UTF-8, ISO-8859-1', true );
|
||||
$data = 'UTF-8' === $encoding ? $data : utf8_encode( $data );
|
||||
$is_valid_utf_8 = mb_check_encoding( $data, 'UTF-8' );
|
||||
if ( ! $is_valid_utf_8 ) {
|
||||
$data = mb_convert_encoding( $data, 'UTF-8', 'ISO-8859-1' );
|
||||
}
|
||||
}
|
||||
|
||||
return $this->escape_data( $data );
|
||||
|
|
Loading…
Reference in New Issue