Validate email column of csv File before insertion in php

纵饮孤独 提交于 2021-01-29 04:22:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!