note 86753 modified in function.imap-fetchstructure by danbrown

From: Date: Sun, 02 Nov 2008 14:48:50 +0000
Subject: note 86753 modified in function.imap-fetchstructure by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146047@lists.php.net to get a copy of this message
I've made this two functions to decode mime headers and so far they are working just fine decoding all headers. Please let me know if you find any error. Hope they are useful for you too. <?php function decode_headers($cabecalho){ while (preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?(.*)$/Ui', $cabecalho)){ $cabecalho = preg_replace("/(\t|\r|\n)/", "", $cabecalho); $cabecalho = preg_replace("/\?(Q|B)\?=/Ui", "?\\1? =", $cabecalho); $partes_cabecalho = explode("?=", $cabecalho); $resultado = ""; foreach ($partes_cabecalho as $texto){ $texto = preg_replace("/\?(Q|B)\?\s=/Ui", "?\\1?=", $texto); $texto = preg_replace("/\?(Q|B)\?/Ui", "=?\\1=?", $texto); if (preg_match('/^(.*)=\?([^?]*)=\?(Q|B)=\?(.*)$/Ui', $texto)){ $partes_texto = explode ("=?", $texto); $parte = descodificar_parte($partes_texto[0], $partes_texto[1]); $parte .= descodificar_parte($partes_texto[3], $partes_texto[2]); } else { $parte = $texto; } if (strtoupper($partes_texto[1]) == "UTF-8"){ $parte = utf8_decode($parte);} $resultado .= $parte; } $cabecalho = $resultado; } return trim($cabecalho); } function descodificar_parte($texto, $codificacao){ if (trim($texto) == ""){ return "";} if (ucfirst($codificacao) == "B"){ $texto = base64_decode($texto); $texto = htmlspecialchars($texto); } else if (ucfirst($codificacao) == "Q"){ $texto = str_replace("_", " ", $texto); $texto = quoted_printable_decode($texto); } return $texto; } ?> --was-- I've made this two functions to decode mime headers and so far they are working just fine decoding all headers. Please let me know if you find any error. Hoppe they are usefull for you too. <? function decode_headers($cabecalho){ while (preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?(.*)$/Ui', $cabecalho)){ $cabecalho = preg_replace("/(\t|\r|\n)/", "", $cabecalho); $cabecalho = preg_replace("/\?(Q|B)\?=/Ui", "?\\1? =", $cabecalho); $partes_cabecalho = explode("?=", $cabecalho); $resultado = ""; foreach ($partes_cabecalho as $texto){ $texto = preg_replace("/\?(Q|B)\?\s=/Ui", "?\\1?=", $texto); $texto = preg_replace("/\?(Q|B)\?/Ui", "=?\\1=?", $texto); if (preg_match('/^(.*)=\?([^?]*)=\?(Q|B)=\?(.*)$/Ui', $texto)){ $partes_texto = explode ("=?", $texto); $parte = descodificar_parte($partes_texto[0], $partes_texto[1]); $parte .= descodificar_parte($partes_texto[3], $partes_texto[2]); } else { $parte = $texto; } if (strtoupper($partes_texto[1]) == "UTF-8"){ $parte = utf8_decode($parte);} $resultado .= $parte; } $cabecalho = $resultado; } return trim($cabecalho); } function descodificar_parte($texto, $codificacao){ if (trim($texto) == ""){ return "";} if (ucfirst($codificacao) == "B"){ $texto = base64_decode($texto); $texto = htmlspecialchars($texto); } else if (ucfirst($codificacao) == "Q"){ $texto = str_replace("_", " ", $texto); $texto = quoted_printable_decode($texto); } return $texto; } ?> http://php.net/manual/en/function.imap-fetchstructure.php

« previous php.notes (#146047) next »