PHP str_replace问题

时间:2011-11-30 19:59:36

标签: php imap str-replace gmail-imap

我有两件事我想在这里做,但我失败了,我已经尝试了一切,所以我不知道原因。

  1. 我正在尝试使用str_replace删除版权,但电子邮件的版权签名正在出现。

  2. 出于某种原因,在消息中的随机位置,它正在输入数字。我真的很困惑为什么会这样。下面是PHP代码以及HTML输出的外观。

  3. ****新产品:**** 我添加了qprint,现在我得到了: enter image description here

    输出: Example

    代码:

    <?php
    
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'username@gmail.com';
    $password = 'Password';
    
    /* try to connect */
    $inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
    
    /* grab emails */
    $emails = imap_search($inbox, 'ALL');
    
    /* if emails are returned, cycle through each... */
    if ($emails) {
    
        /* begin output var */
        $output = '';
    
        /* put the newest emails on top */
        rsort($emails);
    
        /* for every email... */
        foreach ($emails as $email_number) {
    
            /* get information specific to this email */
            $overview = imap_fetch_overview($inbox, $email_number, 0);
            $message = imap_fetchbody($inbox, $email_number, 2);
            $DateFormatted = str_replace("-0500", "", $overview[0] -> date);
    
            /* output the email header information */
            $output .= '<span class="msg_subject">' . $overview[0] -> subject . '</span> | ';
            $output .= '<span class="msg_date"> ' . $DateFormatted . '</span><br />';
    
            $bodyFormatted = str_replace("This e-mail (and attachment(s)) is confidential, proprietary, may be subject to copyright and legal privilege and no related rights are waived. If you are not the intended recipient or its agent, any review, dissemination, distribution or copying of this e-mail or any of its content is strictly prohibited and may be unlawful. All messages may be monitored as permitted by applicable law and regulations and our policies to protect our business. E-mails are not secure and you are deemed to have accepted any risk if you communicate with us by e-mail. If received in error, please notify us immediately and delete the e-mail (and any attachments) from any computer or any storage medium without printing a copy.
    
    Ce courriel (ainsi que ses pièces jointes) est confidentiel, exclusif, et peut faire l’objet de droit d’auteur et de privilège juridique; aucun droit connexe n’est exclu. Si vous n’êtes pas le destinataire visé ou son représentant, toute étude, diffusion, transmission ou copie de ce courriel en tout ou en partie, est strictement interdite et peut être illégale. Tous les messages peuvent être surveillés, selon les lois et règlements applicables et les politiques de protection de notre entreprise. Les courriels ne sont pas sécurisés et vous êtes réputés avoir accepté tous les risques qui y sont liés si vous choisissez de communiquer avec nous par ce moyen. Si vous avez reçu ce message par erreur, veuillez nous en aviser immédiatement et supprimer ce courriel (ainsi que toutes ses pièces jointes) de tout ordinateur ou support de données sans en imprimer une copie.", "", $message);
            /* output the email body */
            $output .= '<span class="msg_body">' . $bodyFormatted . '</span><br /><br />';
    
        }
    
        echo $output;
    }
    
    /* close the connection */
    imap_close($inbox);
    ?>
    

1 个答案:

答案 0 :(得分:2)

我不知道电子邮件的格式是什么,但看起来您需要使用imap_qprint($message);以正确的格式获取邮件,然后执行str_replace('&copy;','',$bodyFormatted);因为它应该是在HTML中。

随机数来自不使用imap_qprint()。您的邮件中有8位字符串需要转换。

以下是修改后的代码:

    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox, $email_number, 0);
    $message = imap_fetchbody($inbox, $email_number, 2);
    $message = imap_qprint($message);
    $DateFormatted = str_replace("-0500", "", $overview[0] -> date);

    /* output the email header information */
    $output .= '<span class="msg_subject">' . $overview[0] -> subject . '</span> | ';
    $output .= '<span class="msg_date"> ' . $DateFormatted . '</span><br />';

    $string = "you should get the HTML and put it in there, I'm sure there are things like &nbsp; and other html chars that it's not finding";
    $bodyFormatted = str_replace($string, "", $message);
    /* output the email body */
    $output .= '<span class="msg_body">' . $bodyFormatted . '</span><br /><br />';