PHP.nl

utf8_decode

utf8_decode

Converts a string from UTF-8 to ISO-8859-1, replacing invalid or unrepresentable characters

string **utf8_decode** string $string

This function converts the string from the encoding to . Bytes in the string which are not valid , and characters which do not exist in (that is, code points above ) are replaced with . string``UTF-8``ISO-8859-1``UTF-8``UTF-8``ISO-8859-1``U+00FF``?

Opmerking: > Many web pages marked as using the character encoding actually use the similar encoding, and web browsers will interpret web pages as . features additional printable characters, such as the Euro sign () and curly quotes ( ), instead of certain control characters. This function will not convert such characters correctly. Use a different function if conversion is required. ISO-8859-1``Windows-1252``ISO-8859-1``Windows-1252``Windows-1252``€``“``”``ISO-8859-1``Windows-1252``Windows-1252

stringA UTF-8 encoded string.

Returns the ISO-8859-1 translation of . string

Voorbeeld: Basic examples

<?php
// Convert the string 'Zoë' from UTF-8 to ISO 8859-1
$utf8_string = "\x5A\x6F\xC3\xAB";
$iso8859_1_string = utf8_decode($utf8_string);
echo bin2hex($iso8859_1_string), "\n";

// Invalid UTF-8 sequences are replaced with '?'
$invalid_utf8_string = "\xC3";
$iso8859_1_string = utf8_decode($invalid_utf8_string);
var_dump($iso8859_1_string);

// Characters which don't exist in ISO 8859-1, such as
// '€' (Euro Sign) are also replaced with '?'
$utf8_string = "\xE2\x82\xAC";
$iso8859_1_string = utf8_decode($utf8_string);
var_dump($iso8859_1_string);
?>
5a6feb
string(1) "?"
string(1) "?"

Opmerking: > ### Deprecation and alternatives

This function is  as of PHP 8.2.0,
and will be removed in a future version. Existing uses should be checked
and replaced with appropriate alternatives.

deprecated

Similar functionality can be achieved with ,
which supports ISO-8859-1 and many other character encodings.

`mb_convert_encoding````php


```php
eb
eb
80
Other options which may be available depending on the extensions installed are
 and .

UConverter::transcode``iconv

The following all give the same result:

Specifying  as the  option
to  gives the same result as
 for strings which are invalid or which can not
be represented in ISO 8859-1.
<?php
$utf8_string = "\x5A\x6F\xC3\xAB"; // 'Zoë' in UTF-8
$iso8859_1_string = utf8_decode($utf8_string);
echo bin2hex($iso8859_1_string), "\n";

$iso8859_1_string = mb_convert_encoding($utf8_string, 'ISO-8859-1', 'UTF-8');
echo bin2hex($iso8859_1_string), "\n";

$iso8859_1_string = iconv('UTF-8', 'ISO-8859-1', $utf8_string);
echo bin2hex($iso8859_1_string), "\n";

$iso8859_1_string = UConverter::transcode($utf8_string, 'ISO-8859-1', 'UTF8');
echo bin2hex($iso8859_1_string), "\n";
?>
5a6feb
5a6feb
5a6feb
5a6feb

`'?''to_subst'UConverter::transcode``utf8_decode````php

'?'] ); var_dump($iso8859_1_string); ?>

```php
sring(1) "?"

utf8_encode``mb_convert_encoding``UConverter::transcode``iconv