theiconic/name-parser
Language-independent PHP name parser that splits full names into parts like salutation, first/middle name, initials, nicknames, last name (incl. prefixes like von/de) and suffixes (Jr/III/PhD). Supports comma formats, multi-language rules, and customizable normalization/whitespace.
Installation
composer require theiconic/name-parser
No additional configuration is required—just autoload the package.
First Use Case
Parse a name string into structured components, including support for combined initials (e.g., TJ Hooker):
use TheIconic\NameParser\NameParser;
$parser = new NameParser();
$result = $parser->parse('TJ Hooker');
// Outputs structured data:
// [
// 'first_name' => 'TJ', // Now correctly parsed as combined initials
// 'middle_name' => null,
// 'last_name' => 'Hooker',
// 'suffix' => null,
// 'prefix' => null,
// 'full_name' => 'TJ Hooker'
// ]
Where to Look First
NameParser class methods (parse(), parseWithOptions()).NameParser::DEFAULT_RULES for built-in heuristics (e.g., suffixes like "Jr.", prefixes like "Dr.").T.J. Hooker, TJ Smith).Basic Parsing (Including Combined Initials)
$parser = new NameParser();
$nameData = $parser->parse('TJ Hooker'); // Now correctly parsed as 'TJ'
$nameData = $parser->parse('T.J. Smith'); // Also handles explicit dots
Custom Rules Override default rules for domain-specific needs:
$customRules = [
'prefixes' => ['Prof.', 'Rev.'],
'suffixes' => ['PhD', 'MD'],
];
$parser = new NameParser($customRules);
$result = $parser->parse('Prof. TJ Garcia PhD');
Integration with Laravel
use Illuminate\Validation\Rule;
$validator->after(function ($validator) {
$name = $validator->getData()['name'];
$parser = new NameParser();
$parsed = $parser->parse($name);
$validator->errors()->merge([
'name' => [
Rule::required()->message('Name is required.'),
Rule::string()->message('Name must be a string.'),
],
]);
});
protected $casts = [
'full_name' => NameParser::class,
];
Batch Processing Parse arrays of names efficiently, including combined initials:
$names = ['Alice', 'TJ Hooker', 'T.J. Smith', 'Bob Jr.'];
$parser = new NameParser();
$results = array_map([$parser, 'parse'], $names);
TJ Hooker and T.J. Hooker formats seamlessly.NameParser and overriding parse().league/address) for robustness in edge cases.Combined Initials Misclassification
TJ Hooker might have been parsed as Tj Hooker (incorrectly splitting TJ).TJ, LL, DJ). No action required unless you relied on the old behavior.Performance with Large Datasets
Edge Cases with Combined Initials
TJJ Hooker or TJJJ Smith may still require validation.if (strlen($parsed['first_name']) > 3 && ctype_alpha($parsed['first_name'])) {
throw new \InvalidArgumentException('First name may be too long or invalid.');
}
var_dump($parser->getRules());
\Log::debug('Parsed name:', $parsedData);
Custom Parsers for Combined Initials
Extend NameParser to enforce stricter rules for combined initials:
class StrictNameParser extends NameParser {
public function parse($name) {
$result = parent::parse($name);
if (strlen($result['first_name']) > 3 && !str_contains($result['first_name'], '.')) {
throw new \RuntimeException('First name too long without dots.');
}
return $result;
}
}
Plugin System for Dynamic Rules Dynamically add rules at runtime, including combined initials validation:
$parser = new NameParser();
$parser->addRule('combined_initials', ['TJ', 'LL', 'DJ']); // Whitelist known valid pairs
Integration with Laravel Services Bind the parser to the container for dependency injection:
$this->app->singleton(NameParser::class, function ($app) {
return new NameParser($app['config']['name_parser.rules']);
});
Handling Mixed Formats Normalize combined initials before processing:
$normalizedName = str_replace(['.', ' '], '', $name); // Convert "T.J. Hooker" to "TJHooker"
$parser->parse($normalizedName);
How can I help you explore Laravel packages today?