Home > Article > Backend Development > How to convert UTF-8 and ISO-8859-1 strings in PHP?
When scripts with different encodings require interaction, it becomes crucial to convert characters between different formats. PHP provides several functions to facilitate such conversions.
To convert a UTF-8 string to ISO-88591, you can use the iconv() or mb_convert_encoding() functions. These functions require specific extensions: ext/iconv for iconv() and ext/mbstring for mb_convert_encoding().
<code class="php">$utf8 = 'ÄÖÜ'; // in a UTF-8 encoded file // Using iconv() $iso88591 = iconv('UTF-8', 'ISO-8859-1', $utf8); // Using mb_convert_encoding() $iso88591 = mb_convert_encoding($utf8, 'ISO-8859-1', 'UTF-8');</code>
To convert an ISO-88591 string to UTF-8, you can also use iconv() or mb_convert_encoding().
<code class="php">$iso88591 = 'ÄÖÜ'; // in an ISO-8859-1 encoded file // Using iconv() $utf8 = iconv('ISO-8859-1', 'UTF-8', $iso88591); // Using mb_convert_encoding() $utf8 = mb_convert_encoding($iso88591, 'UTF-8', 'ISO-8859-1');</code>
The utf8_encode() and utf8_decode() functions may not be suitable for your specific scenario because:
Therefore, using iconv() or mb_convert_encoding() is more appropriate for converting between UTF-8 and ISO-88591.
The above is the detailed content of How to convert UTF-8 and ISO-8859-1 strings in PHP?. For more information, please follow other related articles on the PHP Chinese website!