jasonpriem / HumanNameParser.php

Returns the parts (leading initial, first, middle, last, suffix) of a name from a string.
MIT License
108 stars 41 forks source link

Regex patterns for Suffix and Title need updating #14

Open groggu opened 5 years ago

groggu commented 5 years ago

This is good little parser, but trying it with php 7 I found that the Title and Suffix expressions no longer work as expected. The space between them and the name was not being detected in the pattern so names like "Frank Tester" were becoming "ank Tester"

Here's the update to those patterns. I will try to come back later and do a proper pull request, but for now, here's the update needed in Parse.php

         $nicknamesRegex
             = "/ ('|\"|\(\"*'*)(.+?)('|\"|\"*'*\)) /"; // names that starts or end w/ an apostrophe break this

-        $titleRegex = "/^($titles)/";
-        $suffixRegex = "/,* *($suffixes)$/";
+        $titleRegex = "/^($titles)\s+/";
+        $suffixRegex = "/\,?\s+($suffixes)$/";
         $lastRegex = "/(?!^)\b([^ ]+ y |$prefixes)*[^ ]+$/";
         $leadingInitRegex = "/^(.\.*)(?= \p{L}{2})/"; // note the lookahead, which isn't returned or replaced
         $firstRegex = "/^[^ ]+/"; //