I want to validate all the files that are being uploaded on my site through PHP. I am using regular expressions to compare the file contents but it doesn't seem to be working as I expect it to work. I want to accept files with 1 term per line only.
EXPECTED INPUT:
HP34930
HP09099
HP98899
UNACCEPTABLE INPUT:
HP89980 HP98798 HP09232
some other text
HP58089
Here is my code:
$texthandle = file($_FILES["textfile"]["tmp_name"]);
foreach ($texthandle as $textline)
{
if (!preg_match("/(HP\d+){1}/", $textline))
{
echo "Incorrect file format. Please provide a text file with 1 term per line.";
exit(0);
}
}
Could someone suggest why this isn't detecting the way I want it to?
I have also tried
if (!preg_match("/^(HP\d+){1}$/", $textline))
but it isn't working as I expect it to work.
Thanks for your help!