I'm reading a CSV file and the firstname and last name are together separated by a space. (There could be middle name or middle initial as well, but is being ignored)
Since i'll be doing this hundreds of times (for each line of the CSV), I want to make sure I'm doing this logic as fast as I can. This happens a couple of times PER line.
In English:
Extract firstname and lastname from string
if both are empty, error, else insert the one that's not empty (usually first)
Here's my current code:
[PHP]
// Parse the file
fwrite($logFH, 'Parsing File:\nErrors:\n');
$count = 1;
while (($line = fgetcsv($dataFH, 1000, "|")) !== FALSE)
{
// ------------- ADD SALES PERSON
$employeeName = explode(" ",$line[1]);
$firstName = $employeeName[0];
$lastName = (count($employeeName) > 2) ? end($employeeName) : ""; // assumes last name is at the end, ignores middle name or anything in between
$number = $line[0];
if(!empty($number) && (!empty($firstName) || !empty($lastName)))
{
$employeeVO = new EmployeeVO(INVALID,$number , $firstName, $lastName);
$employeeDAO = new DAO($employeeVO);
}
else
{
fwrite($logFH,'Error On Line: $count\n');
continue;
}
.
.
.
}
// closure code
[/PHP]
I just think all the empty() functions are slowing down.
Is there a quick, one-liner, clean way of doing this logic that I (with a hang over) can't seem to do right now?
Thanks fellas,
Dan