PHP Class FullNameParser

Author: Josh Fraser Contribution from Clive Verrall www.cliveverrall.com February 2016 other contributions: - eric willis list of honorifics - TomThak for raising issue #16 and providing wikepedia resource - atla5 for closing the issue.
Show file Open project: joshfraser/php-name-parser Class Usage Examples

Protected Properties

Property Type Description
$dict - Common honorific prefixes (english) - Common compound surname identifiers - Common suffixes (lineage and professional) Note: longer professional titles should appear earlier in the array than shorter titles to reduce the risk of mis-identification e.g. BEng before BE Also note that case and periods are part of the matching for professional titles and therefore need to be correct, there are no case conversions
$not_nicknames

Public Methods

Method Description
break_words ( string $name ) : array Breaks name into individual words
fix_case ( $word ) # ucfirst all upper/lower strings, but leave camelcase words alone
get_pro_suffix ( string $name ) : mixed Checks for the existence of, and returns professional suffix
parse ( string $name ) : array Parse Static entry point.
parse_name ( $full_name ) : array This is the primary method which calls all other methods
safe_ucfirst ( $seperator, $word ) # helper public function for fix_case

Protected Methods

Method Description
get_nickname ( string $name ) : mixed Function to check name for existence of nickname based on these stipulations - String wrapped in parentheses (string) - String wrapped in double quotes "string" x String wrapped in single quotes 'string'
is_camel_case ( string $word ) : boolean Checks for camelCase words such as McDonald and MacElroy
is_compound ( string $word ) : boolean Checks our dictionary of compound indicators to see if last name is compound
is_initial ( string $word ) : boolean Test string to see if it's a single letter/initial (period optional)
is_line_suffix ( string $word, string $name ) : mixed Checks word against array of common lineage suffixes
is_salutation ( string $word ) : boolean Checks word against list of common honorific prefixes

Method Details

break_words() public method

Breaks name into individual words
public break_words ( string $name ) : array
$name string the full name you wish to parse
return array full list of words broken down by spaces

fix_case() public method

# ucfirst all upper/lower strings, but leave camelcase words alone
public fix_case ( $word )

get_nickname() protected method

I removed the check for strings in single quotes 'string' due to possible conflicts with names that may include apostrophes. Arabic transliterations, for example
protected get_nickname ( string $name ) : mixed
$name string the name you wish to test against
return mixed returns nickname if exists, false otherwise

get_pro_suffix() public method

Checks for the existence of, and returns professional suffix
public get_pro_suffix ( string $name ) : mixed
$name string the name you wish to test
return mixed returns the suffix if exists, false otherwise

is_camel_case() protected method

Checks for camelCase words such as McDonald and MacElroy
protected is_camel_case ( string $word ) : boolean
$word string the single word you wish to test
return boolean

is_compound() protected method

Checks our dictionary of compound indicators to see if last name is compound
protected is_compound ( string $word ) : boolean
$word string the single word you wish to test
return boolean

is_initial() protected method

Test string to see if it's a single letter/initial (period optional)
protected is_initial ( string $word ) : boolean
$word string the single word you wish to test
return boolean

is_line_suffix() protected method

Checks word against array of common lineage suffixes
protected is_line_suffix ( string $word, string $name ) : mixed
$word string the single word you wish to test
$name string full name for context in determining edge-cases
return mixed boolean if false, string if true (returns suffix)

is_salutation() protected method

Checks word against list of common honorific prefixes
protected is_salutation ( string $word ) : boolean
$word string the single word you wish to test
return boolean

parse() public static method

Parse Static entry point.
public static parse ( string $name ) : array
$name string the full name you wish to parse
return array returns associative array of name parts

parse_name() public method

This is the primary method which calls all other methods
public parse_name ( $full_name ) : array
return array returns associative array of name parts

safe_ucfirst() public method

# helper public function for fix_case
public safe_ucfirst ( $seperator, $word )

Property Details

$dict protected property

- Common honorific prefixes (english) - Common compound surname identifiers - Common suffixes (lineage and professional) Note: longer professional titles should appear earlier in the array than shorter titles to reduce the risk of mis-identification e.g. BEng before BE Also note that case and periods are part of the matching for professional titles and therefore need to be correct, there are no case conversions
protected $dict

$not_nicknames protected property

protected $not_nicknames