sonOfRa
sonOfRa

Reputation: 1440

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.

Upvotes: 0

Views: 495

Answers (2)

wroniasty
wroniasty

Reputation: 8052

The . has to be escaped.

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

Upvotes: 3

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

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.

Upvotes: 1

Related Questions