CakeFest 2024: The Official CakePHP Conference

iconv_mime_decode

(PHP 5, PHP 7, PHP 8)

iconv_mime_decodeDecodifica un campo de la cabecera MIME

Descripción

iconv_mime_decode(string $encoded_header, int $mode = 0, string $charset = ini_get("iconv.internal_encoding")): string

Decodifica un campo de la cabecera MIME.

Parámetros

encoded_header

La cabecera codificada, como string.

mode

mode determina el comportamiento cuando el evento iconv_mime_decode() encuentra un campo de cabecera MIME mal formado. Puede especificarse cualquier combinación las siguientes máscaras de bits.

Máscaras de bits aceptadas por iconv_mime_decode()
Valor Constante Descripción
1 ICONV_MIME_DECODE_STRICT Si está definida, la cabecera indicada es decodificada de acuerdo con los estándares definidos en » RFC2047. Esta opción esta deshabilitada por defecto puesto que hay muchos agente de correo incorrectos que no siguen la especificación y no producen las cabeceras MIME correctas.
2 ICONV_MIME_DECODE_CONTINUE_ON_ERROR Si está definida iconv_mime_decode_headers() intenta ignorar cualquier error gramaticar y continúa procesando la cabecera dada.

charset

El parámetro opcional charset especifica el set de caracteres a usar para representar el resultado. Si se omite, se usará iconv.internal_encoding.

Valores devueltos

En caso de éxito retorna un campo MIME decodificado o false si ocurre cualquier error durante la decodificación.

Ejemplos

Ejemplo #1 Ejemplo de la función iconv_mime_decode()

<?php
// Esto contiene "Subject: Prüfung Prüfung"
echo iconv_mime_decode("Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=",
0, "ISO-8859-1");
?>

Ver también

add a note

User Contributed Notes 3 notes

up
3
Dirk Becker
10 years ago
While creating a new webmailer, I had to coop with a lot of mails and only half of them were correct encoded!
Often the text is tagged as ISO but in real its UTF :/

After trying a lot of solutions and combination a found a way which seems to work for all our mails. Maybe its usefull to someone else too.

<?php

function mime_encode($data)
{
$resp = imap_utf8(trim($data));

if(
preg_match("/=\?/", $resp))
$resp = iconv_mime_decode($data, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "ISO-8859-15");

if(
json_encode($resp) == 'null')
$resp = utf8_encode($resp);

return
$resp;
}

?>
up
1
koronci at aol dot com
11 years ago
A simple and working solution for latin encoding supports Slovak, Czech, Russian ect.
<?php iconv("utf-8", "windows-1250", $SomeWeirdText); ?>

specially for those who strugle with imap_mime_header_decode
up
1
dido dot sevilla at gmail dot com
19 years ago
In PHP versions that have imap_mime_decode built in, it's possible to emulate the operation of this function:

<?php
function iconv_mime_decode($str, $mode=0, $charset="UTF-8")
{
$data = imap_mime_header_decode($str);
if (
count($data) > 0) {
// because iconv doesn't like the 'default' for charset
$charset = ($data[0]->charset == 'default') ? 'ASCII' : $data[0]->charset;
return(
iconv($charset, $charset, $data[0]->text));
}
return(
"");
}
?>

I've only tried to use this code snippet to decode ISO-2022-JP messages to UTF-8, but I see no reason why it shouldn't work in other cases.
To Top