Zend - The PHP Company




Arrays

Add Code


Name Splitter  

Type: code fragment
Added by: aopchuck
Entered: 11/01/2004
Last modified: 01/12/2003
Rating: - (fewer than 3 votes)
Views: 3752
This takes a full name and converts it into an array. It drops the middle initial, if present, to render the first and last name.


<?php
$name 
preg_split("/[s]+/""$FullName");

       if (empty(
$name[2]))  // If there's no third name present, 
                             //we'll assume we've received
                       // only the first and last name
          
{
          
$FirstName= ("$name[0]");
        
$LastNamestripslashes("$name[1]"); // stripslashes because 
                                               //we love the Irish
          
}
       else  
// If there is a third name present, 
             // we'll assume the 2nd position is the 
           // middle initial, which we'll drop for the form entry.  
          
{
          
$FirstName= ("$name[0]");  // Allow multiple name entries 
                         // so Jay L. De La Cruz, Jr. doesn't 
                                     // feel slighted. 
        
$LastNamestripslashes(("$name[2]" " "$name[3].  " "  "$name[4]" " "$name[5]"));
          }
?>


Usage Example




Rate This Script





Search



This Category All Categories