Question:
How to clean up my Excel File (CSV, XLSX, ...) Database in PHP in my case?

Problem

I have three different (3) Excel files (in .xlsx, .csv format) whose Header containing the Email column is placed in different positions (sometimes in the 2nd place, sometimes in the 5th or even 7th place) on each of the files.


Knowing that the two pieces of code that I am trying to use to delete all the rows whose Email columns are not valid are the following:


// Validate Emails with PHP Coding:

if (filter_var($email_address, FILTER_VALIDATE_EMAIL) === false || preg_match('/@.+\./', $email_address) === false) {

    // email is not valid syntax

}



// Check Email Domain MX Record with PHP (capture domain extension): 

$email_host = strtolower(substr(strrchr($email_address, "@"), 1));

// set host to fully qualified domain:

$email_host = idn_to_ascii($email_host.'.');

if (!checkdnsrr($email_host, "MX")) {

    // email invalid -> domain does not have a valid MX record

}


Can you help me combine the two codes above which check the validity of Email Server Names in order to clean (by deleting all the rows whose Email columns are not valid) my database (knowing how all Excel databases do not have header level, the Email column at the same position) ???


Solutions

You need to move that logic to a function.


function isEmailValid(string $email_address): bool

{

    // Validate Emails with PHP Coding:

    if (filter_var($email_address, FILTER_VALIDATE_EMAIL) === false || preg_match('/@.+\./', $email_address) === false) {

        return false;

    }



    // Check Email Domain MX Record with PHP (capture domain extension): 

    $email_host = strtolower(substr(strrchr($email_address, "@"), 1));

    // set host to fully qualified domain:

    $email_host = idn_to_ascii($email_host.'.');

    if (!checkdnsrr($email_host, "MX")) {

        return false;

    }

    

    return true;

}


Note, that I didn't inspect any of your actual logic for email validation, I just wrapped it.


Answered by: >Chris Haas

Credit: >StackOverflow


Blog Link:

>How to extract tags from TMX (XML) content using PHP?

>Php Error Solved- Instance deployment failed to install Composer dependencies

>How to do PHP gd Installation on Ubuntu?

>How to Create an array based on two other arrays in Php?

>What are common syntax errors and exceptions in Python?

>Python: Export "fat" Cocoa Touch Framework (for Device and Simulator)

>Python Import Error Solved: Cannot Import Name X

>Python ValueError Solved: ‘Shape must be rank 1 but is rank 0’


Nisha Patel

Nisha Patel

Submit
0 Answers