使用PHP从电子邮件中提取数据时出现额外字符问题

使用PHP从电子邮件中提取数据时出现额外字符问题,php,html,Php,Html,我正在使用localhost、php5.6.40、windows10进行开发,并使用I.E.11、Chrome或MS-Edge测试结果。 我正在从电子邮件中提取数据。 数据包含在电子邮件正文中,我使用以下方法提取数据: $structure = imap_fetchstructure($inbox, $email_number); if(isset($structure->parts) && is_array($structure-&g

我正在使用localhost、php5.6.40、windows10进行开发,并使用I.E.11、Chrome或MS-Edge测试结果。 我正在从电子邮件中提取数据。 数据包含在电子邮件正文中,我使用以下方法提取数据:

           $structure = imap_fetchstructure($inbox, $email_number);

       if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1]))
           {
               echo '<BR><BR>INSIDE OF STRUCTURE SECTION:';
               $part = $structure->parts[1];
               $message = imap_fetchbody($inbox,$email_number,2);
                if($part->encoding == 3)
                   {
                        $message = imap_base64($message);
                   } else if($part->encoding == 1) 
                     {
                             $message = imap_8bit($message);
                           } else {
                              $message = imap_qprint($message);
                           }
           }   // end of if(isset
$structure=imap\u fetchstructure($inbox,$email\u number);
if(isset($structure->parts)&&is_数组($structure->parts)&&isset($structure->parts[1]))
{
回显“

结构段内部:”; $part=$structure->parts[1]; $message=imap\u fetchbody($inbox,$email\u number,2); 如果($part->encoding==3) { $message=imap_base64($message); }else if($part->encoding==1) { $message=IMAP8bit($message); }否则{ $message=imap_qprint($message); } }//如果结束(isset)
我相信,由于我在从电子邮件中提取$message并显示数据后立即显示$message,并且由于“=”出现在该点上,因此它与编码或类似的内容有关

数据可以用html标记以三种方式之一封装:

  • 在一张普通的桌子上
  • 
    04/21/2021
    
  • 在具有样式的表格中,即
  • 
    04/21/2021 
    
  • 在具有样式并用段落样式包围数据的表中(调用类) i、 e
  • 
    2021年1月4日

    我唯一在正确提取数据方面遇到问题的是第三个表,它有一个样式和围绕数据的段落类。包含这种类型结构的电子邮件是由MS Office自动生成的(不知道是什么版本)

    我可以从第三种类型的表(表样式和段落样式)中提取数据,但它总是向数据中添加额外的字符。 例如:2021年12月4日 变为:04/=01/2021 或 这个:131.64 变为:131=.64

    它仅对具有样式的表和围绕数据的段落类(称为段落类)执行此操作。 如果我打印一份硬拷贝来检查数据,它不包含额外的字符。 无论我在执行应用程序时使用哪种浏览器,它都会执行相同的操作。它会插入额外的字符。当我使用开发工具DOM Console时,无论我使用哪种浏览器,它都不会显示所有尚未删除的html标记。它以前会显示,现在不会显示。 这是什么原因造成的?我如何纠正? 我最初提取电子邮件正文时是否使用了错误的“解码”类型?
    感谢您的帮助。

    在对编码/解码进行了深入和长期的研究之后,我发现MS Office使用了一个字符集windows-1252。我在原始代码中添加了以下代码,到目前为止,它解决了我的问题

     $message = mb_convert_encoding($message, "Windows-1252", "UTF-8");
    
    由于并非所有电子邮件都是在MS Office中生成的,因此我添加了编码以检查windows-1252字符集。下面是修改后的编码:

            if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1]))
                {
                    $part = $structure->parts[1];
                    $message = imap_fetchbody($inbox,$email_number,2);
                    
                    $strChkForOffice = 'windows-1252';
                    
                    if (substr_count($message,$strChkForOffice) > 0)
                      {
                        $message = mb_convert_encoding($message, "Windows-1252", "UTF-8");
                      }else{
                         if($part->encoding == 3)
                   {
                        $message = imap_base64($message);
                    } else if($part->encoding == 1) 
                    {
                        $message = imap_8bit($message);
                    } else {
                        $message = imap_qprint($message);
                    }
                }   
            }   // end of if(isset
    
        <table class="MsoNormalTable"
        style="border-collapse:collapse;border:none" cellspacing="0" cellpadding="0" border="1">
        <tbody>
        <tr>
        <td style="border-top:none;border-left:none;border-bottom:solid
        black 1.0pt;border-right:solid black 1.0pt;padding:.75pt .75pt .75pt .75pt" valign="top">
        <p class="MsoNormal"><span style="font-size:10.5pt">04/01/2021<o:p></o:p></span></p>
        </td>
        </tr>
        </tbody>
        </table>
    
     $message = mb_convert_encoding($message, "Windows-1252", "UTF-8");
    
            if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1]))
                {
                    $part = $structure->parts[1];
                    $message = imap_fetchbody($inbox,$email_number,2);
                    
                    $strChkForOffice = 'windows-1252';
                    
                    if (substr_count($message,$strChkForOffice) > 0)
                      {
                        $message = mb_convert_encoding($message, "Windows-1252", "UTF-8");
                      }else{
                         if($part->encoding == 3)
                   {
                        $message = imap_base64($message);
                    } else if($part->encoding == 1) 
                    {
                        $message = imap_8bit($message);
                    } else {
                        $message = imap_qprint($message);
                    }
                }   
            }   // end of if(isset