如果包含某个单词,则取消设置数组值

时间:2018-02-12 20:16:31

标签: php arrays unset

如果数组中包含某个单词,我想取消它的设置值。我正在做的是,我从gmail收件箱中提取所有电子邮件,只显示来自邮件正文的电子邮件,以便我可以在数据库中更新它。消息正文包含很少的电子邮件,因为它是未发送电子邮件的。

当显示所有电子邮件时,它还会显示我不希望发送电子邮件的位置。

代码如下:

/*===================================   GMAIL   ======================================*/
/* connect to gmail */
    //$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'username@gmail.com';
    $password = 'password';

    //$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    //Which folders or label do you want to access? - Example: INBOX, All Mail, Trash, labelname 
    //Note: It is case sensitive
    $imapmainbox = "INBOX";

    //Select messagestatus as ALL or UNSEEN which is the unread email
    $messagestatus = "ALL";

    //-------------------------------------------------------------------

    //Gmail Connection String
    $imapaddress = "{imap.gmail.com:993/imap/ssl}";

    //Gmail host with folder
    $hostname = $imapaddress . $imapmainbox;

    //Open the connection
    $connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    //Grab all the emails inside the inbox
    $emails = imap_search($connection,$messagestatus);

    //number of emails in the inbox
    $totalemails = imap_num_msg($connection);

    echo "Total Emails: " . $totalemails . "<br>";

    if($emails) {

      //sort emails by newest first
      rsort($emails);

      //loop through every email int he inbox
      foreach($emails as $email_number) {

        //grab the overview and message
        $header = imap_fetch_overview($connection,$email_number,0);

        //Because attachments can be problematic this logic will default to skipping the attachments    
        $message = imap_fetchbody($connection,$email_number,1.1);
             if ($message == "") { // no attachments is the usual cause of this
              $message = imap_fetchbody($connection, $email_number, 1);
        }

        //split the header array into variables
        $status = ($header[0]->seen ? 'read' : 'unread');
        $subject = $header[0]->subject;
        $from = $header[0]->from;
        $date = $header[0]->date;

        preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $message, $matches);
        //$matches = array_unique($matches);

        // remove some of the emails which i dont want to include
        $matches[0] = remove(remove(remove(array_unique($matches[0]) ,  'info@example.co.uk') ,  'no-reply@example.co.uk'),  '131669395@infong353.kundenserver.de') ;

        foreach($matches[0] as $email) {                                                        
          //echo $email . "<br>";
            echo '
                <div class="alert alert-primary" role="alert">
                    '.$email.' in <b>'.category_name(cat_id_from_email($email)).'</b> group <br><br>
                    <a href="index.php?p=gmail&undelivered='.$email.'" class="btn btn-info nounderline" >Undelivered</a>
                    <a href="index.php?p=gmail&unsubscribers='.$email.'" class="btn btn-warning nounderline" >Unsubscribers</a>
                    <a href="index.php?p=gmail&delete='.$email.'" class="btn btn-danger nounderline" >Delete</a>
                </div>
        ';
        }
      }  
    } 

    // close the connection
    imap_close($connection);

我还想删除包含某些域名的任何电子邮件地址。例如:

如果任何电子邮件是xxxxx@example2.com,我想取消设置。所以基本上是example2.com的任何电子邮件地址。

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

我可以看到两种方式;在显示循环之前从数组中删除:

$matches[0] = preg_grep('/example2\.com/', $matches[0], PREG_GREP_INVERT);

或者检查显示循环,只是不显示:

foreach($matches[0] as $email) {
    if(strpos($email, 'example2.com') !== false) { continue; }
    //echo your stuff
}

取决于您是否要保持数组完整,但不显示或修改数组以供以后使用。

相关问题