NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page [PHP] Initialing Names with PHP

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 03-16-2010, 10:24 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!

Cool [PHP] Initialing Names with PHP


Hi,

Sometimes you may want to initial names with PHP, So instead of Adrian John Crepaz, just display A. J. Crepaz.
There is various reasons why you may want to do this, so here is a function we wrote for you.

PHP Code:
<?php

/*
* str_initial function
* This will take a string (Presumably a name), and initial it.
* It will initial either All names, Middle Names or Forenames.

* Example, To Initial Adrian John Crepaz
* * INT_ALL        would return A. J. C.
* * INT_MIDDLE     would return Adrian J. Crepaz
* * INT_FORE       would return A. J. Crepaz

* Unlimited amounts of names supported, as some people have 4 or 5 names in the full name.
*
* Seperator
* * Enter 1 in the seperator argument to split names with a period .
* * Enter 0 in the seperator argument to just space names out with no period.

* Writted By Cueburst
* * http://cueburst.com
*/

error_reporting(E_ALL & ~E_NOTICE);

define('INT_ALL'1);
define('INT_MIDDLE'2);
define('INT_FORE'3);

function 
str_initial($name$type '3'$seperate 1)
{
    
/* Types
    *
    *  1 - All
    *  2 - Middle
    *  3 - Fornames
    */
    
    
if(empty($name) OR !isset($name))
    {
        die(
'No name specified for str_initial()');
    }
    
    
$valid_types = array('1''2''3');
    if(!
in_array($type$valid_types))
    {
        die(
'Invalid initial type for str_initial()');
    }
    
    
/*
    * Seperate the Words into an array
    * Capitalize each word
    * Replace %20 with a space, incase the name is passed through a url ($_GET)
    * Create the seperator
    */
    
    
$name str_replace('%20'' '$name);
    
$names explode(' 'ucwords($name));
    
$sep = ($seperate == 1) ? '. ' ' ';
????: NamePros.com http://www.namepros.com/code/645243-php-initialing-names-with-php.html
    
    
$total count($names);
    
    
// All
    
if($type == '1')
    {
        foreach(
$names AS $single)
        {
            
$result .= substr($single01) . $sep;
        }
        
        return 
$result;
    }
    
    
// Middle
    
else if($type == '2')
    {
        if(
$total <= 2)
        {
            return 
$name;
        }
        else if(
$total >= 2)
        {
            
$middle $total-2;
            
$result $names[0] . $sep;
            
            
$int 1;
            while(
$int <= $middle)
            {
                
$result .= substr($names[$int], 01) . $sep;
                
$int++;
            }
            
            
$result .= $names[$total-1];
        }
        
        return 
$result;
    }
    
    
// Forename
    
else if($type == '3')
    {
        if(
$total == 1)
        {
            return 
$name;
        }
        else if(
$total == 2)
        {
            return 
substr($names[0], 01) . $sep ' ' $names[1];
        }
        else if(
$total >= 3)
        {
            
$int 0;
            while(
$int $total-1)
            {
                
$result .= substr($names[$int], 01) . $sep;
                
$int++;
            }

            
$result .= $names[$total-1];

            return 
$result;
        }
    }
}

?>
Using the function
PHP Code:
<?php

require_once 'str_initial.php';

$name 'Adrian John Crepaz';
echo 
str_initial($nameINT_FORE);

?>
Final Notes
This function will support unlimited amount of names, as some people have 4 or 5 names in their full name.

There is 3 types of Initialing supported, these are... (For example, we are initialing Adrian John Crepaz)
  • INT_ALL would return A. J. C.
  • INT_MIDDLE would return Adrian J. Crepaz
  • INT_FORE would return A. J. Crepaz

Entering 1 or 0 in the seperator argument will add a period (.) between each initials, or not. (1 to add period, 0 to enter a space).

Thats pretty much all there is to it.

For updates, please check the post over at the Cueburst Blog.
????: NamePros.com http://www.namepros.com/showthread.php?t=645243

Enjoy.
Last edited by Hitch; 03-16-2010 at 11:31 AM.
Hitch is offline   Reply With Quote
Old 04-06-2010, 12:36 PM   #2 (permalink)
NamePros Regular
 
sourcez's Avatar
Join Date: Nov 2007
Location: UK
Posts: 403
sourcez is a jewel in the roughsourcez is a jewel in the roughsourcez is a jewel in the rough
 



Nice share

Only comment would be that some names (just looked through a users table) need to have lowercase, or should be written lowercase.

For example,
George van der Kraft
becomes
George Van Der Kraft

Just nitpicking
__________________
3cc Internet
sourcez is offline   Reply With Quote
Reply

Tags
cueburst, initial names, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 07:32 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger