I'm using an csv file to upload students email address to db using laravel. CSV file contains one column named 'email' to list emails. I want to add a validation to check whther all emails are in correct format or not. correct email address format should be @uni.com
please advice
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function ($reader) {
})->get();
if (!empty($data) && $data->count() && !empty($data1) && $data1->count() ) {
foreach ($data as $key => $value) {
$insert[] = ['email' => $value->email]; }
if (!empty($insert)) {
DB::table('users')->insert($insert);
}
I'm using laravel maatwebsite/excel plugin
You can use use a Regular Expression check in PHP. See http://php.net/manual/en/function.preg-match.php for reference.
Suppose email addresses can have any character from a to z, the following PHP code will check for correct email format (Generally email addresses have underscores, digits (not as the first character) and so on.. so you need to make a more complex regex to test for it)
$pattern = '/[a-z]+@uni\.com/i';
if(preg_match($pattern, $email) == 1) {
// Correct email address
} else {
// Incorrect email address
}