Given a string
$str= "name1 surname2, name2 midname2 surname2";
There are two persons, one has two values (name, surname) while the other has a middle name too. I want to get all of them separately while knowing which name belongs to which person, like:
foreach ($persons as person){
if( person has midname){
$value1 ="name"; $value2= "midname"; $value3="surname"
}
else
$value1="name"; $value2="surname"
}
PHP’s explode() will help you out a lot:
Edit
As rightly noted by dm03514: this will not work if the person has a space in any of their names (i.e. a last name of ‘De Luca’). However, this is as close as you’re going to get without some sort of restrictions or validation enforced at the source of the names.