I'm using this PHP regexp to check true/false whether a field contains a name, consisting of at least a first/last name, and then optional other middle names or initials.
$success = preg_match("/([\x{00c0}-\x{01ff}a-zA-Z'-]){2,}(\s([\x{00c0}-\x{01ff}a-zA-Z'-]{1,})*)?\s([\x{00c0}-\x{01ff}a-zA-Z'-]{2,})/ui",$user['name'],$matches);
$output[($success ? 'hits' : 'misses')][] = ['id' => $user['id'],'email' => $user['email'],'name' => $user['name'],'matches' => $matches];
Seems to work fine in terms of hits/misses, i.e. true/false whether it matches or not.
But then I'm trying to use the same thing to extract the first and last names using groups, which I'm struggling to get right..
Get lots of results like:
"name": "Jonny Nott",
"matches": [
"Jonny Nott",
"y",
"",
"",
"Nott"
]
"name": "Name Here",
"matches": [
"Name Here",
"e",
"",
"",
"Here"
]
"matches": [
"Jonathan M Notty",
"n",
" M",
"M",
"Notty"
]
..but what I really want is for one of the 'matches' to always contain just the first name, and one to contain always just the last name.
Any pointers as to what's wrong?