Select last name except First Initial

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
RonH
Forum Newbie
Posts: 6
Joined: Thu Mar 01, 2012 2:39 pm

Select last name except First Initial

Post by RonH »

Right now I have a list of first and Last names, one name per row. Each name is separated by a space, I want to keep the Fist letter of the last name and erase the rest and place a period at the end.

i.e. Tom Smith -> Tom S.

Ron
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Select last name except First Initial

Post by requinix »

Sounds reasonable. What's your question?

Just in case it's "will somebody give me the answer?" then allow me to reply preemptively with "no, but we'll help you if you're having problems with your own attempt at it".
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Select last name except First Initial

Post by Christopher »

There are several ways to do this. Here is one way: Use strpos() to find the position of space and then substr() to truncate the string.
(#10850)
RonH
Forum Newbie
Posts: 6
Joined: Thu Mar 01, 2012 2:39 pm

Re: Select last name except First Initial

Post by RonH »

Absolutely, I'm not really looking for a hand out I want to understand what I'm doing.

I've tried this
\s\w+\n

But I'm not sure how to start the selection after the space-first character (In the last name).
What I have highlights the space and the last name.

Ron
RonH
Forum Newbie
Posts: 6
Joined: Thu Mar 01, 2012 2:39 pm

Re: Select last name except First Initial

Post by RonH »

I really would like to do this with Regex if I can.

Ron
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Select last name except First Initial

Post by requinix »

Regex isn't the best tool for it but whatever.

Start at the space, capture the next non-space character, and read until the end of the string. Replace that with a space and the character you captured.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Select last name except First Initial

Post by Christopher »

Christopher wrote:There are several ways to do this. Here is one way: Use strpos() to find the position of space and then substr() to truncate the string.
substr($name, 0, strpos($name, ' ')+2) . '.'
(#10850)
Post Reply