|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
blackhawkmx Smarty Regular
Joined: 20 May 2005 Posts: 76
|
Posted: Sat Sep 05, 2009 2:12 am Post subject: exclude all middile initials with or without a per |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Sep 06, 2009 1:30 am Post subject: |
|
|
Something like (php):
Code: | $name = preg_replace('!(\w+) \w\. (\w+)!','$1 $2',$name); |
|
|
Back to top |
|
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Tue Sep 08, 2009 11:14 am Post subject: Function to remove middle initials |
|
|
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 |
|
mitchenall Smarty Pro
Joined: 27 Feb 2004 Posts: 107 Location: London, UK
|
Posted: Tue Sep 08, 2009 12:04 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Sep 08, 2009 2:35 pm Post subject: |
|
|
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 |
|
jpiercemaine Smarty Regular
Joined: 24 Sep 2009 Posts: 82
|
Posted: Fri Sep 25, 2009 7:27 pm Post subject: why not explode? |
|
|
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 |
|
|
|
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
|