I am trying to validate the emails that come to me from a textarea, assuming that several fields separated by tabs can come in the textarea, just as it is possible that only the email comes, the only mandatory field that must come in the textarea is the email .
The problem is that if I send only the email without the name, it does not validate the email and takes it as wrong.
The mail begins to take it as wrong from the second line and if I send it with only the mail, I have the textarea prepared to receive 5 fields separated by tab, which is how it is copied from an excel.
Here is the code where I validate the email:
<?php
public function validarEmail()
{
//certifico que el textarea llega con todos sus campos a este punto
$rows = preg_split("/[\n]/", $this->textarea);
$contador = 0;
$fallo = 0;
$array = array();
$array2 = array();
$totales = array();
foreach ($rows as $row) {
$email = $name = $lastName = $address = $phone = '';
list($email, $name , $lastName , $address , $phone) = (explode("\t",$row), 5, null);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$contador = $contador + 1;
array_push($array, $email);
}
else {
$fallo = $fallo + 1;
array_push($array2, $email);
}
}
$totales = array($contador, $fallo);
return array($totales, $array, $array2, $rows);
}
What should I do so that regardless of the list of emails I send, if only the email comes, it validates it correctly, the same for the following fields?
An example for the file to follow would be the following:
Apart from the option to remove spaces, I also included some improvements to the code: