从imap_fetchbody解析样式

时间:2013-08-16 21:14:49

标签: php parsing imap

具体来说,我想保留换行信息。这是我的代码:

$body = imap_fetchbody($stream, $emailId, 1.2); 
if (!strlen($body) > 0) { 
    $body = imap_fetchbody($stream, $emailId, 1.1); 
}
if (!strlen($body) > 0) {
    $body = imap_fetchbody($stream, $emailId, 1);
}

$structure = imap_fetchstructure($stream, $emailId);
if ($structure->encoding == 3) {
    $body = base64_decode($body);
} else if ($structure->encoding == 4) {
    $body = imap_qprint($body);
} else if ($structure->encoding == 0) {
    $body = $this->decode7Bit($body);
}

private function decode7Bit($text)
{
    // If there are no spaces on the first line, assume that the body is
    // actually base64-encoded, and decode it.
    $lines = explode("\r\n", $text);
     $first_line_words = explode(' ', $lines[0]);
    if ($first_line_words[0] == $lines[0]) {
        $text = base64_decode($text);
    }

    // Manually convert common encoded characters into their UTF-8 equivalents.
    $characters = array(
        '=20' => ' ', // space.
        '=E2=80=99' => "'", // single quote.
        '=0A' => "\r\n", // line break.
        '=A0' => ' ', // non-breaking space.
        '=C2=A0' => ' ', // non-breaking space.
        "=\r\n" => '', // joined line.
        '=E2=80=A6' => '…', // ellipsis.
        '=E2=80=A2' => '•', // bullet.
    );

    // Loop through the encoded characters and replace any that are found.
    foreach ($characters as $key => $value) {
        $text = str_replace($key, $value, $text);
    }

    return $text;
}

我还将正文存储到数据库中。当我通过phpMyAdmin查看数据库中的正文时,它具有样式!不是可见标签,但文本实际上被分解为段落。但是,每当我转储正文文本或将其回显到html中时,它都没有任何样式。这是一个巨大的在线运行。我在这里做错了什么?

0 个答案:

没有答案