Jump to content

Name-capturing regex


silkfire

Recommended Posts

I've hit a standstill with a regex I'm developing it seems pretty simple but it's not really working :/

 

It needs to capture a name and grab the very first name and make initials out of the following names (middle and last names).

 

Regex right now looks like:

 

preg_match_all('#^([^\s]+)(?: (.)[^\s]+)+$#m', $string, $matches, PREG_SET_ORDER);

 

I want the regex to work both with multiline and single line (I'm testing on a big list of names separated with newline).

 

So if the name is George Walker Bush I want to capture George W B. If it's Laura Bush, I want Laura B, if it's George Herbert Walker Bush I want George H W B, etc.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/230511-name-capturing-regex/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.