phpday 2027 - Call for Papers

utf8_encode

(PHP 4, PHP 5, PHP 7, PHP 8)

utf8_encode — Converte una stringa da ISO-8859-1 a UTF-8

Avviso

Questa funzione è stata DEPRECATA a partire da PHP 8.2.0. Fare affidamento su questa funzione è altamente sconsigliato.

Descrizione

#[\Deprecated(
    since: '8.2',
    message: 'visit the php.net documentation for various alternatives',
)]

function utf8_encode(string $string): string

Questa funzione converte la stringa string dalla codifica ISO-8859-1 alla codifica UTF-8.

Nota:

Questa funzione non tenta di indovinare la codifica corrente della stringa fornita: presume che sia codificata in ISO-8859-1 (nota anche come "Latin 1") e la converte in UTF-8. Poiché ogni sequenza di byte è una stringa ISO-8859-1 valida, non si verifica mai un errore, ma il risultato non sarà una stringa utile se era prevista una codifica diversa.

Molte pagine web dichiarate come codificate in ISO-8859-1 usano in realtà la codifica simile Windows-1252, e i browser web interpretano le pagine ISO-8859-1 come Windows-1252. Windows-1252 include ulteriori caratteri stampabili, come il simbolo dell'euro (€) e le virgolette tipografiche (“ ”), al posto di alcuni caratteri di controllo di ISO-8859-1. Questa funzione non converte correttamente tali caratteri Windows-1252. Se è necessaria la conversione da Windows-1252, occorre usare un'altra funzione.

Elenco dei parametri

string

Una stringa ISO-8859-1.

Valori restituiti

Restituisce la traduzione in UTF-8 di string.

Log delle modifiche

Versione Descrizione
8.2.0 Questa funzione è stata deprecata.
7.2.0 Questa funzione è stata spostata dall'estensione XML al nucleo di PHP. Nelle versioni precedenti era disponibile solo se l'estensione XML era installata.

Esempi

Example #1 Esempio di base

<?php
// Converte la stringa 'Zoë' da ISO 8859-1 a UTF-8
$iso8859_1_string = "\x5A\x6F\xEB";
$utf8_string = utf8_encode($iso8859_1_string);
echo bin2hex($utf8_string), "\n";
?>

Il precedente esempio visualizzerà:

5a6fc3ab

Note

Nota: Deprecazione e alternative

Questa funzione è deprecata a partire da PHP 8.2.0 e verrà rimossa in una versione futura. Gli utilizzi esistenti dovrebbero essere verificati e sostituiti con alternative appropriate.

Una funzionalità simile si ottiene con mb_convert_encoding(), che supporta ISO-8859-1 e molte altre codifiche di caratteri.

<?php
$iso8859_1_string = "\xEB"; // 'ë' (e con dieresi) in ISO-8859-1
$utf8_string = mb_convert_encoding($iso8859_1_string, 'UTF-8', 'ISO-8859-1');
echo bin2hex($utf8_string), "\n";

$iso8859_7_string = "\xEB"; // la stessa stringa in ISO-8859-7 rappresenta 'λ' (lettera lambda minuscola greca)
$utf8_string = mb_convert_encoding($iso8859_7_string, 'UTF-8', 'ISO-8859-7');
echo bin2hex($utf8_string), "\n";

$windows_1252_string = "\x80"; // '€' (simbolo dell'euro) in Windows-1252, ma non in ISO-8859-1
$utf8_string = mb_convert_encoding($windows_1252_string, 'UTF-8', 'Windows-1252');
echo bin2hex($utf8_string), "\n";
?>

Il precedente esempio visualizzerà:

c3ab
cebb
e282ac

Altre opzioni che possono essere disponibili a seconda delle estensioni installate sono UConverter::transcode() e iconv().

Tutti i seguenti esempi danno lo stesso risultato:

<?php
$iso8859_1_string = "\x5A\x6F\xEB"; // 'Zoë' in ISO-8859-1

$utf8_string = utf8_encode($iso8859_1_string);
echo bin2hex($utf8_string), "\n";

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

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

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

Il precedente esempio visualizzerà:

5a6fc3ab
5a6fc3ab
5a6fc3ab
5a6fc3ab

Vedere anche:

  • utf8_decode() - Converte una stringa da UTF-8 a ISO-8859-1, sostituendo i caratteri non validi o non rappresentabili
  • mb_convert_encoding() - Convert a string from one character encoding to another
  • UConverter::transcode() - Convert a string from one character encoding to another
  • iconv() - Converte una stringa da una codifica di caratteri a un'altra

User Contributed Notes 4 notes

up
141
deceze at gmail dot com
15 years ago
Please note that utf8_encode only converts a string encoded in ISO-8859-1 to UTF-8. A more appropriate name for it would be "iso88591_to_utf8". If your text is not encoded in  ISO-8859-1, you do not need this function. If your text is already in UTF-8, you do not need this function. In fact, applying this function to text that is not encoded in ISO-8859-1 will most likely simply garble that text.

If you need to convert text from any encoding to any other encoding, look at iconv() instead.
up
11
Aidan Kehoe <php-manual at parhasard dot net>
22 years ago
Here's some code that addresses the issue that Steven describes in the previous comment; 

<?php

/* This structure encodes the difference between ISO-8859-1 and Windows-1252,
   as a map from the UTF-8 encoding of some ISO-8859-1 control characters to
   the UTF-8 encoding of the non-control characters that Windows-1252 places
   at the equivalent code points. */

$cp1252_map = array(
    "\xc2\x80" => "\xe2\x82\xac", /* EURO SIGN */
    "\xc2\x82" => "\xe2\x80\x9a", /* SINGLE LOW-9 QUOTATION MARK */
    "\xc2\x83" => "\xc6\x92",     /* LATIN SMALL LETTER F WITH HOOK */
    "\xc2\x84" => "\xe2\x80\x9e", /* DOUBLE LOW-9 QUOTATION MARK */
    "\xc2\x85" => "\xe2\x80\xa6", /* HORIZONTAL ELLIPSIS */
    "\xc2\x86" => "\xe2\x80\xa0", /* DAGGER */
    "\xc2\x87" => "\xe2\x80\xa1", /* DOUBLE DAGGER */
    "\xc2\x88" => "\xcb\x86",     /* MODIFIER LETTER CIRCUMFLEX ACCENT */
    "\xc2\x89" => "\xe2\x80\xb0", /* PER MILLE SIGN */
    "\xc2\x8a" => "\xc5\xa0",     /* LATIN CAPITAL LETTER S WITH CARON */
    "\xc2\x8b" => "\xe2\x80\xb9", /* SINGLE LEFT-POINTING ANGLE QUOTATION */
    "\xc2\x8c" => "\xc5\x92",     /* LATIN CAPITAL LIGATURE OE */
    "\xc2\x8e" => "\xc5\xbd",     /* LATIN CAPITAL LETTER Z WITH CARON */
    "\xc2\x91" => "\xe2\x80\x98", /* LEFT SINGLE QUOTATION MARK */
    "\xc2\x92" => "\xe2\x80\x99", /* RIGHT SINGLE QUOTATION MARK */
    "\xc2\x93" => "\xe2\x80\x9c", /* LEFT DOUBLE QUOTATION MARK */
    "\xc2\x94" => "\xe2\x80\x9d", /* RIGHT DOUBLE QUOTATION MARK */
    "\xc2\x95" => "\xe2\x80\xa2", /* BULLET */
    "\xc2\x96" => "\xe2\x80\x93", /* EN DASH */
    "\xc2\x97" => "\xe2\x80\x94", /* EM DASH */

    "\xc2\x98" => "\xcb\x9c",     /* SMALL TILDE */
    "\xc2\x99" => "\xe2\x84\xa2", /* TRADE MARK SIGN */
    "\xc2\x9a" => "\xc5\xa1",     /* LATIN SMALL LETTER S WITH CARON */
    "\xc2\x9b" => "\xe2\x80\xba", /* SINGLE RIGHT-POINTING ANGLE QUOTATION*/
    "\xc2\x9c" => "\xc5\x93",     /* LATIN SMALL LIGATURE OE */
    "\xc2\x9e" => "\xc5\xbe",     /* LATIN SMALL LETTER Z WITH CARON */
    "\xc2\x9f" => "\xc5\xb8"      /* LATIN CAPITAL LETTER Y WITH DIAERESIS*/
);

function cp1252_to_utf8($str) {
        global $cp1252_map; 
        return  strtr(utf8_encode($str), $cp1252_map);
}

?>
up
2
Mark AT modernbill DOT com
22 years ago
If you haven't guessed already: If the UTF-8 character has no representation in the ISO-8859-1 codepage, a ? will be returned. You might want to wrap a function around this to make sure you aren't saving a bunch of ???? into your database.
up
0
mmjunior at live dot com
2 months ago
utf8_encode_alt() replaces the deprecated utf8_encode(). 
Converts an ISO-8859-1 string to UTF-8.

utf8_decode_alt replaces the deprecated utf8_decode(). 
Converts a UTF-8 string to ISO-8859-1.

Using mb_convert_encoding().

<?php 

function utf8_encode_alt(string $string): string
{
    return mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1');
}

function utf8_decode_alt(string $string): string
{
    return mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');
}
?>

The same using iconv().

<?php

function utf8_encode_alt(string $string): string
{
    return iconv('ISO-8859-1', 'UTF-8', $string);
}

function utf8_decode_alt(string $string): string
{
    return iconv('UTF-8', 'ISO-8859-1', $string);
}

?>

My recommendation: Use mb_convert_encoding(). The mbstring extension is standard in virtually all modern PHP installations. The iconv() launches error by failure, and behavior varies by OS.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top