jQuery使用相同的数组查找和删除数组中的字符串

时间:2015-11-13 17:35:18

标签: javascript jquery arrays email

我问了这个问题:Javascript/jQuery find and delete array value in the rest of array,但我觉得我解释错了。这是我的第二次尝试。

我使用PHP的imap函数来提取电子邮件。我能够成功地发送电子邮件。下面是代码片段,展示了如何对其进行编码。

foreach($all_email_dates as $email){
    $email_id = explode(":", $email);
    $email_id = $email_id[1];
    $email = imap_fetchbody($inbox, $email_id, 1.1, FT_UID);
    if($email == "") $email = imap_fetchbody($inbox, $email_id, 1, FT_UID);
    if(imap_base64($email)) $email = imap_base64($email);
    $email = quoted_printable_decode($email);
    $email = preg_replace('/(^\w.+:\n)?(^>.*(\n|$))+/mi', '', $email);
    array_push($emails, $email);
    echo $email;
    echo "<:>";
}

这会提取我需要的所有电子邮件,并按时间顺序排列。

现在,我使用AJAX调用抓住它们并尝试在这里操作它们:

$.post('php/contacts-get-email.php', {email:email}, function(data) {
    var email_body = data.split("<:>"); //Puts each email into an array
    console.log(email_body);
    //I have test code here for now that is not working
});

我想要做的是遍历该数组(email_body)并找到并删除任何重复的字符串。上面的输出如下所示:http://jsfiddle.net/exdjumqm/。 (我把它放在一个小提琴而不是这里,因为这里显示的时间太长了)--- ADD 这里也是一个JSON文件:http://412webdesigns.com/uploads/emails.json

主要目标是删除以前在电子邮件中的任何文字。如果可能的话,我想保留回车以进行格式化。

例如,第一封电子邮件中有一段文字显示在电子邮件2和4中。我希望删除该文本。但我不知道从哪里开始。

如果您需要更多详情,请告诉我们。谢谢

更新 以下是粘贴在这里的电子邮件字符串:

第一封电子邮件

Thanks so much name! We had a great time today too and can't wait to 
see\r\nwhat the pictures look like! Looking forward to seeing them when you're 
all\r\ndone editing! Thanks again :)\r\n

第二封电子邮件

Hi name!\r\n\r\n\r\n\r\nThank you for the email! You don't have to wait any 
further!\r\n\r\n\r\n\r\nWe were able to get around 70 pictures that we think 
were noteworthy! To  \r\ndownload ALL of your pictures, head to 
www.site.com\/download  \r\nand use the password \"AN5k9W\". This should 
automatically start your  \r\ndownload, so check your downloads folder. It's a 
half of a gig, so the file  \r\nis pretty large!\r\n\r\n\r\n\r\nTo check out 
some of the pics while you're waiting on the download, head to  
\r\nhttp:\/\/site.com\/#\/151108-name_name-engagement or  
\r\nfacebook.com\/site. Remember to like us and share your pics  \r\nwith 
anyone you want!\r\n\r\n\r\n\r\nLet us know what you think!\r\n\r\nWe look 
forward to henameng from you!\r\n\r\nname & name\r\n\r\n\r\n\r\nOn Sun, Nov 8, 
2015 at 2:52 PM name name <name@gmail.com>  \r\nwrote:\r\n\r\n\r\nThanks so 
much name! We had a great time today too and can't wait to see  \r\nwhat the 
pictures look like! Looking forward to seeing them when you're all  \r\ndone 
editing! Thanks again :)\r\n

您会注意到第二封电子邮件包含第一封电子邮件,因为它是对第一封电子邮件的回复。

3 个答案:

答案 0 :(得分:1)

var formatted_email_body = []; // Init a blank array
var email_body = data.split("<:>");

for(var i=0; i<email_body.length; i++)
{
   if(formatted_email_body.indexOf(email_body[i]) == -1) // If not in the new array
       formatted_email_body.push(email_body[i]); // Push it
}

现在formatted_email_body是一个只有唯一值的数组。

答案 1 :(得分:0)

由于这个问题如此稀释,我现在想给出一个对我有用的答案......

我已经回到了php并使用了一些&#34;规则&#34;减少电子邮件垃圾。以下是有兴趣的规则:

//Get your email
$email = imap_fetchbody($inbox, $email_id, 1.1, FT_UID); //check body types
if($email == "") $email = imap_fetchbody($inbox, $email_id, 1, FT_UID);// check body types
if(imap_base64($email)) $email = imap_base64($email);//decode base64 if it is
$email = quoted_printable_decode($email); //Decode the Email
$email = explode("\r\n\r\nOn", $email); //split the email if the reply starts with "On this day, this person wrote"
$email = explode("\r\n>", $email[0]); //Split the email again if the line starts with a line break and "less than" sign (>) which usually denotes a reply 
$email = str_replace("\r\n\r\n", '<br />', $email[0]); //Give normal line breaks to double carriage returns and newlines
$email = str_replace("\r", ' ', $email); //Give spaces to unused carriage returns
echo $email;

答案 2 :(得分:0)

根据你的评论,我把一个函数uniqParagraphs放在一起。希望你觉得它很有用:)

arr = ["Thanks so much name! We had a great time today too and can't wait to see\r\nwhat the pictures look like! Looking forward to seeing them when you're all\r\ndone editing! Thanks again :)\r\n","Hi name!\r\n\r\n\r\n\r\nThank you for the email! You don't have to wait any further!\r\n\r\n\r\n\r\nWe were able to get around 70 pictures that we think were noteworthy! To  \r\ndownload ALL of your pictures, head to www.site.com\/download  \r\nand use the password \"AN5k9W\". This should automatically start your  \r\ndownload, so check your downloads folder. It's a half of a gig, so the file  \r\nis pretty large!\r\n\r\n\r\n\r\nTo check out some of the pics while you're waiting on the download, head to  \r\nhttp:\/\/site.com\/#\/151108-name_name-engagement or  \r\nfacebook.com\/site. Remember to like us and share your pics  \r\nwith anyone you want!\r\n\r\n\r\n\r\nLet us know what you think!\r\n\r\nWe look forward to henameng from you!\r\n\r\nname & name\r\n\r\n\r\n\r\nOn Sun, Nov 8, 2015 at 2:52 PM name name <name@gmail.com>  \r\nwrote:\r\n\r\n\r\nThanks so much name! We had a great time today too and can't wait to see  \r\nwhat the pictures look like! Looking forward to seeing them when you're all  \r\ndone editing! Thanks again :)\r\n\r\n","name and name,\r\nWe LOVE them!!  We can't thank you guys enough!  I definitely think we're\r\nready to talk wedding prices! :)\r\n\r\nname\r\n\r\nOn Wed, Nov 11, 2015 at 6:52 PM, Grealish Photography <\r\nus@site.com> wrote:\r\n\r\n","Great name!! We're so happy you love them! And thank you for being such  \r\ngreat models!\r\n\r\n\r\n\r\nWe have you penciled in for September 24th, 2016 so far. To get it booked  \r\npermanently, we should schedule a time for us to sit down together and have  \r\na pre-wedding consultation. During the pre-wedding consultation, we'll go  \r\nover any details you may have for the wedding, go over the contract and  \r\nthen exchange the deposit. The deposit will be 50% of the full cost of our  \r\nservices.\r\n\r\n\r\n\r\nTo figure out the cost, I'll just need you to let me know how much time you  \r\nneed us for and where the wedding is located. This will determine the cost  \r\nand the deposit.\r\n\r\n\r\n\r\nTo break down our pricing, we charge for our time at the event, time  \r\nediting (it's about 1:1, wedding:editing), and our time traveling. So a 6  \r\nhour wedding with 1 hour of travel time is actually 13 hours of work on our  \r\nend.\r\n\r\n\r\n\r\nLet me know what you think and how much time you're thinking for your  \r\nwedding (as well as where it's located) and I can give you a final price!\r\n\r\n\r\n\r\nWe look forward to henameng from you!\r\n\r\nname & name\r\n\r\n\r\n\r\nOn Wed, Nov 11, 2015 at 10:09 PM name name <name@gmail.com>  \r\nwrote:\r\n\r\n\r\nname and name,\r\nWe LOVE them!! We can't thank you guys enough! I definitely think we're  \r\nready to talk wedding prices! :)\r\n\r\n\r\nname\r\n\r\n\r\n\r\nOn Wed, Nov 11, 2015 at 6:52 PM, Grealish Photography  \r\n<us@site.com> wrote:\r\n\r\n\r\nHi name!\r\n\r\n\r\n\r\nThank you for the email! You don't have to wait any further!\r\n\r\n\r\n\r\nWe were able to get around 70 pictures that we think were noteworthy! To  \r\ndownload ALL of your pictures, head to www.site.com\/download  \r\nand use the password \"AN5k9W\". This should automatically start your  \r\ndownload, so check your downloads folder. It's a half of a gig, so the file  \r\nis pretty large!\r\n\r\n\r\n\r\nTo check out some of the pics while you're waiting on the download, head to  \r\nhttp:\/\/site.com\/#\/151108-name_name-engagement or  \r\nfacebook.com\/site. Remember to like us and share your pics  \r\nwith anyone you want!\r\n\r\n\r\n\r\nLet us know what you think!\r\n\r\nWe look forward to henameng from you!\r\n\r\nname & name\r\n\r\n\r\nOn Sun, Nov 8, 2015 at 2:52 PM name name <name@gmail.com>  \r\nwrote:\r\n\r\n\r\nThanks so much name! We had a great time today too and can't wait to see  \r\nwhat the pictures look like! Looking forward to seeing them when you're all  \r\ndone editing! Thanks again :)\r\n\r\n\r\n"]
uniq = {};
uniqParagraphs = function(e) {
  $a = e.match( /[^\r\n]+[\r\n\r\n]+/g ); // split into paragraphs

  $($a).each( function (index, sentence) {
    // calc one way hash for trim'ed text
    // these are smaller uniq values for large texts
    key = calcMD5(sentence.trim());
    // else use a very large key too
    // key = sentence.trim();
    if (uniq[key] !== undefined) { // if para already exists, remove it
      $a[index] = "";
    } else {
      uniq[key] = true; // add para to uniq dictionary
    }
  });

  return $a.join(""); // join everything again
}

$(arr).each( function(i, e) {
  arr[i] = uniqParagraphs(e); // update arr[i]
});

这是一个jsfiddle,以防:http://jsfiddle.net/exdjumqm/1/