I sometimes will get off on a rabit trail while trying to do 
something. That's what happened when I came up with the script below. 
It is designed to return a "properly" formated surname from user 
input. The issues include hyphenated last names, names with 
punctuation (St. John, O'Riley), and names with capitalization after 
a prefix (MacGeorge, McElroy, LaMonde). Somethings to consider with 
it are: The name that is passed should be checked with your normal 
user input checks first (htmlspecialcharacters, etc). I hope someone 
finds this useful, and if you can break it or suggest ways to improve 
it, I'll be glad to hear about it.

<html>
<head>
<title></title>
</head><body>
<?
function fmtname ($name)
{
$nameLength=strlen($name);
$afterperiod=strpos($name,".");
$afterspace=strpos($name," ");

$afterdash=strpos($name,"-");
$afterapos=strpos($name,"'");
$splitString=preg_split('//',$name,-1,PREG_SPLIT_NO_EMPTY);

$firstTwo=$splitString[0].$splitString[1];
$firstThree=$firstTwo.$splitString[2];

if ($afterspace===false)
  {
//no space found in the name
  }
  else
  {
$n=$afterspace+1;
$splitString[$n]=strtoupper($splitString[$n]);
  }
if ($afterdash===false)
  {
//no dash found in the name
  }
else
  {
$n=$afterdash+1;
$splitString[$n]=strtoupper($splitString[$n]);
  }
if ($afterperiod===false)
  {
//no period was found in the name
  }
else
  {
$n=$afterperiod+1;
$splitString[$n]=strtoupper($splitString[$n]);
  }
if ($afterapos===false)
  {
//no apostophe was found in the name
  }
else
  {
$n=$afterapos+1;
$splitString[$n]=strtoupper($splitString[$n]);
  }
if ($firstTwo!="Mc")
  {
//The first two letters are not Mc
  }
  else
  {
$splitString[2]=strtoupper($splitString[2]);
  }
if (($firstTwo=="El" or $firstTwo=="La" or $firstTwo=="Le") and 
($splitString[2]!=" "))
  {
$retname="";
for ($i=0;$i<$nameLength;$i++)
    {$retname.=$splitString[$i];
    }
$splitString[2]=strtoupper($splitString[2]);
$retname2="";
for ($i=0;$i<$nameLength;$i++)
    {$retname2.=$splitString[$i];
    }
  }
  elseif (($firstThree=="Mac" or $firstThree=="Del") and ($splitString
[3]!=" "))
  {
$retname="";
for ($i=0;$i<$nameLength;$i++)
    {$retname.=$splitString[$i];
    }
$splitString[3]=strtoupper($splitString[3]);
$retname2="";
for ($i=0;$i<$nameLength;$i++)
    {$retname2.=$splitString[$i];
    }
  }
  else
  {
$retname="";
for ($i=0;$i<$nameLength;$i++)
    {$retname.=$splitString[$i];
    }
$retname2="";
  }

return array ($retname,$retname2);
}
if (!$submit)
{
?>
<form method="post" action="<? echo"$PHP_SELF"; ?>">
<table width="80%" align="center">
<tr><td align="right">Name to check: </td>
<td><input name=nameToCheck></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" 
value="Check This Name"></td></tr></table>
</form>
<?
}
else
{
$nameToCheck=ucfirst(strtolower($_POST[nameToCheck]));
$nameToChekck=trim($nameToCheck);
list ($opt1,$opt2)=fmtname($nameToCheck);
if ($opt2=="")
  {
echo"<p style=\"text-align:center;\">The name formats 
to:<br>$opt1</p>";
  }
else
  {
echo"<p style=\"text-align:center;\">You will have to choose 
between<br>
<input type=radio name=pickname value='$opt1'>$opt1<br>
<input type=radio name=pickname value='$opt2'>$opt2</p>";
  }
echo"<p style=\"text-align:center;\"><a href=nametest.php>Try 
Again</a></p>";
}
?>
</body>
</html>





Community email addresses:
  Post message: [email protected]
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-list/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to