Regex:
'#(?P<ref>.+\d) (?P<name>\w+ [A-Z ]*\w+) (?P<city>.+)#'
- First capture anything before the name which ends in a single digit. I'm unsure if this is correct because of lack of examples / format for ref no. If this is incorrect remove the space between ".+" and "\d". Store with key 'ref' in array.
- Capture a name with 0 or more middle names. Store with key 'name' in array.
- Capture anything after the name as city name. Store with key 'city' in array.
Try this:
$vars = array(
'2E4 766 06 7982 647 5 Joesph Sanchez Zuni',
'2E4 766 06 7982 647 5 Joesph J Sanchez Zuni',
'2E4 766 06 7982 647 5 Joesph J G Sanchez Zuni',
'2E4 766 06 7982 647 5 Joesph Sanchez Los Angeles',
'2E4 766 06 7982 647 5 Joesph J Sanchez Los Angeles',
'2E4 766 06 7982 647 5 Joesph J G Sanchez Los Angeles',
'2E4 766 06 7982 647 5 Joesph Sanchez St. Morel',
'2E4 766 06 7982 647 5 Joesph J Sanchez St. Morel',
'2E4 766 06 7982 647 5 Joesph J G Sanchez St. Morel',
);
$matches = array();
foreach ($vars as $var) {
if (preg_match('#(?P<ref>.+ \d) (?P<name>\w+ [A-Z ]*\w+) (?P<city>.+)#', $var, $matches)) {
echo 'Ref: ', $matches['ref'], '. Name: ', $matches['name'], '. City: ', $matches['city'], "\n";
} else {
echo "No match for $var\n";
}
}
Result:
Ref: 2E4 766 06 7982 647 5. Name: Joesph Sanchez. City: Zuni
Ref: 2E4 766 06 7982 647 5. Name: Joesph J Sanchez. City: Zuni
Ref: 2E4 766 06 7982 647 5. Name: Joesph J G Sanchez. City: Zuni
Ref: 2E4 766 06 7982 647 5. Name: Joesph Sanchez. City: Los Angeles
Ref: 2E4 766 06 7982 647 5. Name: Joesph J Sanchez. City: Los Angeles
Ref: 2E4 766 06 7982 647 5. Name: Joesph J G Sanchez. City: Los Angeles
Ref: 2E4 766 06 7982 647 5. Name: Joesph Sanchez. City: St. Morel
Ref: 2E4 766 06 7982 647 5. Name: Joesph J Sanchez. City: St. Morel
Ref: 2E4 766 06 7982 647 5. Name: Joesph J G Sanchez. City: St. Morel