如果它包含特定单词,则取消设置数组值

Unset array value if it contains certain word

如果其中包含某个单词,我想取消设置数组值。我正在做的是,我从 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.

的电子邮件地址

谁能帮我解决这个问题。

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

$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
}

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