What is the recommended method for extracting and storing names from a string with specific prefixes and suffixes in PHP?

When extracting and storing names from a string with specific prefixes and suffixes in PHP, one recommended method is to use regular expressions to match the patterns of the names. By defining the prefixes and suffixes in the regular expression pattern, you can extract the names that meet the specified criteria. Once the names are extracted, you can store them in an array or any other data structure for further processing.

$string = "Mr. John Doe and Mrs. Jane Smith are attending the event.";
$pattern = '/(?:Mr\.|Mrs\.) [A-Z][a-z]+ [A-Z][a-z]+/';

preg_match_all($pattern, $string, $matches);

$names = $matches[0];

print_r($names);