Skip to content
PHP Logo

How can PHP handle different Unicode formats, such as UTF-8 or UTF-16, to ensure compatibility with other applications like Excel?

To handle different Unicode formats like UTF-8 or UTF-16 in PHP, you can use the `mb_convert_encoding` function to convert strings between different encodings. This ensures compatibility with other applications like Excel that may require a specific Unicode format for data processing.

php
// Convert a string from UTF-8 to UTF-16
$utf8String = "Hello, 你好";
$utf16String = mb_convert_encoding($utf8String, 'UTF-16', 'UTF-8');

// Convert a string from UTF-16 to UTF-8
$utf16String = "H\x00e\x00l\x00l\x00o\x00,\x00 \x00\x4F60\x00\x4F60";
$utf8String = mb_convert_encoding($utf16String, 'UTF-8', 'UTF-16');

echo $utf8String; // Output: Hello, 你好