Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

exclude all middile initials with or without a per

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
blackhawkmx
Smarty Regular


Joined: 20 May 2005
Posts: 76

PostPosted: Sat Sep 05, 2009 2:12 am    Post subject: exclude all middile initials with or without a per Reply with quote

Question
how can i tell regex_replace to exclude all middle initials in a name?

i.e. larry t. brown or larry t brown should reas as larry brown

thanks!
_________________
Bring it back to basics. You and I can make it right!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Sep 06, 2009 1:30 am    Post subject: Reply with quote

Something like (php):

Code:
$name = preg_replace('!(\w+) \w\. (\w+)!','$1 $2',$name);
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Sep 08, 2009 11:14 am    Post subject: Function to remove middle initials Reply with quote

dot (.) looks to be an optional part. Hence, need to append ?. Please see the regex portion: \w\.? for this. Here is a full code.
Code:
<?php

# mi = middle initials

function remove_mi($name='')
{
   $name = preg_replace('!(\w+) \w\.? (\w+)!','$1 $2',$name);
   return($name);
}

# Testing...
$names = array(
   'larry t. brown',
   'larry t brown',
);

foreach($names as $i => $name)
{
   $name_nomi = remove_mi($name);
   echo("\n{$name}: {$name_nomi}");
}
?>
Back to top
View user's profile Send private message Visit poster's website
mitchenall
Smarty Pro


Joined: 27 Feb 2004
Posts: 107
Location: London, UK

PostPosted: Tue Sep 08, 2009 12:04 pm    Post subject: Reply with quote

Seeing as there is no standard to how people write their names, neither of the above is correct.

What about people with double-barrelled names? What about people with multiple middle initials? Worse, people who actually use their second name, and have the initial at the start (J. Edgar Hoover). Extreme, perhaps, but this isn't the only time I've seen a name presented in this way.

So, what are you actually looking to remove? Take the following examples...

John A Smith
John A. Smith
John A.W. Smith
John AW Smith
J. Alan W Smith

Only the first two are matched with the above regex's. Do people have multiple middle initials? Well, I do, and I know of plenty of others.

You also don't specify whether your names are properly capitalised. If they are, writing an effective regex might be easier.
_________________
Mark Mitchenall
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Sep 08, 2009 2:35 pm    Post subject: Reply with quote

Yes it is nearly impossible to catch every type of name situation. My regex is meant to be a guide to get going in the right direction. What about no middle name? Multiple spaces? 4, 5 or more names? To match any of these:

Code:
John Smith
John A Smith
John A. Smith
John A.W. Smith
John AW Smith
J. Alan W Smith
Billy Joe Jim A. Bob
Fee     Foo     Fum


You can try (untested):

Code:
$name = preg_replace('!((?:[\w\.]\s+)*[\w\.]+)\s+(?:\w\.?)?\s+([\w\.]+)!','$1 $2',$name);


This assumes you always want to remove the 2nd from last name.
Back to top
View user's profile Send private message Visit poster's website
jpiercemaine
Smarty Regular


Joined: 24 Sep 2009
Posts: 82

PostPosted: Fri Sep 25, 2009 7:27 pm    Post subject: why not explode? Reply with quote

Split the white space and concat:

PHP:
Code:

$nameArr=explode(' ', $name);
$lastindex=count($nameArr)-1;
$newname=$nameArr[0] + ' ' + $nameArr[$lastindex];


Untested![/code]
_________________
~ jpierce
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP