Domain Empire

[PHP] Initialing Names with PHP

Spaceship Spaceship
Watch
Impact
74
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:
<?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) ? '. ' : ' ';
	
	$total = count($names);
	
	// All
	if($type == '1')
	{
		foreach($names AS $single)
		{
			$result .= substr($single, 0, 1) . $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], 0, 1) . $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], 0, 1) . $sep . ' ' . $names[1];
		}
		else if($total >= 3)
		{
			$int = 0;
			while($int < $total-1)
			{
				$result .= substr($names[$int], 0, 1) . $sep;
				$int++;
			}

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

			return $result;
		}
	}
}

?>

Using the function
PHP:
<?php

require_once 'str_initial.php';

$name = 'Adrian John Crepaz';
echo str_initial($name, INT_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.

Enjoy.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
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 :P
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back