问题
Hi i have used regular expression for validating email columns of csv file before insertion in php.but when i used regular expression or filter validate email function it always show emails in incorrect format although i have a correct email format in csv file.I want that it read a csv file and check email column in csv file and check its value i.e in email should be in correct format or not.If it is correct format then the data insert otherwise skip.
回答1:
Use PHP FILTER_VALIDATE_EMAIL Filter like:
<?php
$email = "john.doe@example.com";
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
回答2:
you might like to filter the file outside of php, then insert it.
You could use grep like
grep -E "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file.txt > file-out.txt
But its worth pointing out that even though an email might match the regex it might still be invalid. You could use a service like Real Email to validate the files for you. That way you can check if the email address actually exists and is active.
来源:https://stackoverflow.com/questions/39867552/validate-email-column-of-csv-file-before-insertion-in-php