Home  >  Article  >  Backend Development  >  How do I Convert UTF-8 Characters to ISO-8859-1 and Vice Versa in PHP?

How do I Convert UTF-8 Characters to ISO-8859-1 and Vice Versa in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 00:21:02942browse

How do I Convert UTF-8 Characters to ISO-8859-1 and Vice Versa in PHP?

Converting UTF-8 Characters to ISO-8859-1 and Vice Versa in PHP

Issue Description

Encodings can become problematic when combining scripts that use different types. While changing the encoding of individual scripts is not possible, there is a need to convert the result from one script (UTF-8) into ISO-8859-1 format to be used as a parameter in another script.

Solution

Three PHP functions offer solutions to this conversion: iconv(), mb_convert_encoding(), and (less commonly used) utf8_encode().

iconv() and mb_convert_encoding()

These functions provide similar functionality:

iconv: iconv('source_encoding', 'target_encoding', $string)

mb_convert_encoding: mb_convert_encoding($string, 'target_encoding', 'source_encoding')

Example:

<code class="php">$utf8 = 'ÄÖÜ';
$iso88591 = iconv('UTF-8', 'ISO-8859-1', $utf8);</code>

utf8_encode()

utf8_encode encodes an ISO-8859-1 string to UTF-8:

<code class="php">$iso88591 = 'ÄÖÜ';
$utf8 = utf8_encode($iso88591);</code>

Note that utf8_decode() is not suitable for this conversion.

The above is the detailed content of How do I Convert UTF-8 Characters to ISO-8859-1 and Vice Versa in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn