Search code examples
phpregexpreg-split

preg_split() Regex for Initials Lastname


I have a name pattern looking like this:

F. O. O. Bar
F. Oobar
F. O. Obar

I'm currently trying to develop a regex that lets me split names in firstname, maybe initials and surname according to one of these.

foreach($authors as $author) {
    $arr = preg_split("/([a-zA-Z]. )+/", $author, -1, PREG_SPLIT_DELIM_CAPTURE);
    //Do stuff with $arr
}

However, this also splits Foo. Bar (or to be exact o.). The problem is that I cannot limit it to lowercase only, as the data I have incoming are VERY inconsistent, so I cannot rely on this.


Solution

  • You mean you only want to allow one letter before the dot? Use a word boundary to ensure this:

    $arr = preg_split("/\b([a-zA-Z]\. )+/", $author, PREG_SPLIT_DELIM_CAPTURE);
    

    Also, as wroniasty correctly noted, the dot needs escaping.