2011-11-30 123 views
0

我有兩件事情我想在這裏做,但我很悲慘失敗,我已經嘗試了一切,所以我不知道原因。PHP str_replace問題

  1. 我試圖用str_replace刪除版權,但電子郵件的版權簽名出現。

  2. 出於某種原因,在消息中的隨機點,它正在輸入數字。我真的很困惑爲什麼會這樣。以下是PHP代碼以及HTML輸出的外觀。

****新的輸出:**** 我加入了QPRINT,現在我得到: enter image description here

輸出: Example

代碼:

<?php 

/* connect to gmail */ 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$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); 
?> 
+0

的「隨機字符串」似乎編碼值。它們不是隨機的,而是編碼版本的'''''''''和其他重音字母。 – Qqwy

+0

它在甚至沒有重音字母的地方添加數字。它需要一定的空間並用這些數字取代它們。 – Bulvak

+0

例如,原始電子郵件:請注意,沃爾德代爾/蒙特利爾數據中心(SDE)將於12月6日/ 7日(沃爾德代爾)和12月12日/ 13日進行網絡維護(蒙特利爾)。在此維護期間,預計不會停機。範圍:由於在受影響的路由器上發現問題,需要緊急網絡維護。影響:媒體基礎架構上的所有託管網站,包括但不限於(可能即將進行的遷移): – Bulvak

回答

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 />'; 
+0

你能編輯和粘貼我有點困惑的代碼片段嗎?當用戶通過電子郵件發送給我時,他們的簽名代碼包含版權簽名等,因此它顯示在html上。 你說str_replace('©','',$ bodyFormatted);但是什麼是&copy – Bulvak

+0

©是'''符號的HTML編碼版本。但是我不明白這是如何解決你的HTML問題的。 – Qqwy

+0

我想刪除整個副本正確的段落..不希望它顯示當我回聲的HTML。我應該在我的代碼中編輯什麼:S? – Bulvak