简体   繁体   中英

Unset array value if it contains certain word

I want to unset an array value if it contains a certain word in it. What I am doing is that, I am pulling all the emails from gmail inbox and only displaying the emails from the the message body so i can update it in the database. Message body includes few emails as it is for undelivered emails.

When all the emails are displayed, it also displays where the email was sent from which i dont want.

The code is below:

/*===================================   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);

I also want to remove any email address with certain domain names. For example:

if any of the email is xxxxx@example2.com, i want to unset it. So basically any email address with example2.com.

Could anyone help me with this.

I can see two ways; remove from the array before the display loop:

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

Or check in the display loop and just don't display:

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

Depends on if you want to keep the array intact but just not display or modify the array for later use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM