I have a code that separates first from last names, that I am using on a form that could have up to ten names. Right now I have simply replicated the code multiple times, manually updating the number, but figure there has to be a way to loop this function.
The basic code is:
if (!empty($_POST['name2'])) {
$name2 = ucwords(strtolower($_POST['name2']));
$parser = new HumanNameParser_Parser($name2);
$fname2 = $parser->getFirst();
$lname2 = $parser->getLast();
}
As the fname and lname variables are used elsewhere I need to stick with the naming.
I was thinking to use a For:
for ($i=2; $i <= 6; $i++)
{
if (!empty($_POST['name($i)'])) {
$name($i) = ucwords(strtolower($_POST['name($i)']));
$parser = new HumanNameParser_Parser($name($i));
$fname($i) = $parser->getFirst();
$lname($i) = $parser->getLast();
}
else
{
break;
}
}
But I am having a tough time figuring what I should actually use for the ($i) in my example. I tried .$i and [$i] but keep getting errors. And not sure if the way I should code it in the $_POST would be different than the variable $name.