PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 19, 2014 3:39 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Jun 06, 2012 9:55 pm 
Offline
Forum Newbie

Joined: Thu Mar 01, 2012 3:39 pm
Posts: 6
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


Top
 Profile  
 
PostPosted: Wed Jun 06, 2012 10:13 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5701
Location: WA, USA
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".


Top
 Profile  
 
PostPosted: Wed Jun 06, 2012 10:18 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 12722
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Wed Jun 06, 2012 10:34 pm 
Offline
Forum Newbie

Joined: Thu Mar 01, 2012 3:39 pm
Posts: 6
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


Top
 Profile  
 
PostPosted: Thu Jun 07, 2012 10:13 pm 
Offline
Forum Newbie

Joined: Thu Mar 01, 2012 3:39 pm
Posts: 6
I really would like to do this with Regex if I can.

Ron


Top
 Profile  
 
PostPosted: Thu Jun 07, 2012 11:11 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5701
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Fri Jun 08, 2012 2:54 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 12722
Location: New York, NY, US
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)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group