Page 1 of 1

Select last name except First Initial

Posted: Wed Jun 06, 2012 9:55 pm
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

Re: Select last name except First Initial

Posted: Wed Jun 06, 2012 10:13 pm
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".

Re: Select last name except First Initial

Posted: Wed Jun 06, 2012 10:18 pm
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.

Re: Select last name except First Initial

Posted: Wed Jun 06, 2012 10:34 pm
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

Re: Select last name except First Initial

Posted: Thu Jun 07, 2012 10:13 pm
by RonH
I really would like to do this with Regex if I can.

Ron

Re: Select last name except First Initial

Posted: Thu Jun 07, 2012 11:11 pm
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.

Re: Select last name except First Initial

Posted: Fri Jun 08, 2012 2:54 pm
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) . '.'