_wp_utf8_encode_fallback()
Converts a string from ISO-8859-1 to UTF-8, maintaining backwards compatibility with the deprecated function from the PHP standard library.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
String. Text converted into UTF-8.
Usage
_wp_utf8_encode_fallback( $iso_8859_1_text );
- $iso_8859_1_text(string) (required)
- Text treated as ISO-8859-1 (latin1) bytes.
Notes
- See: \utf8_encode()
Changelog
| Since 6.9.0 | Introduced. |
_wp_utf8_encode_fallback() wp utf8 encode fallback code WP 6.9.1
function _wp_utf8_encode_fallback( $iso_8859_1_text ) {
$iso_8859_1_text = (string) $iso_8859_1_text;
$at = 0;
$was_at = 0;
$end = strlen( $iso_8859_1_text );
$utf8 = '';
while ( $at < $end ) {
// US-ASCII bytes are identical in ISO-8859-1 and UTF-8. These are 0x00–0x7F.
$ascii_byte_count = strspn(
$iso_8859_1_text,
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" .
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" .
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f",
$at
);
if ( $ascii_byte_count > 0 ) {
$at += $ascii_byte_count;
continue;
}
// All other bytes transform into two-byte UTF-8 sequences.
$code_point = ord( $iso_8859_1_text[ $at ] );
$byte1 = chr( 0xC0 | ( $code_point >> 6 ) );
$byte2 = chr( 0x80 | ( $code_point & 0x3F ) );
$utf8 .= substr( $iso_8859_1_text, $was_at, $at - $was_at );
$utf8 .= "{$byte1}{$byte2}";
++$at;
$was_at = $at;
}
if ( 0 === $was_at ) {
return $iso_8859_1_text;
}
$utf8 .= substr( $iso_8859_1_text, $was_at );
return $utf8;
}