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.
Strengths:
TJ Hooker → now correctly parsed as given_name: TJ, family_name: Hooker). This improves accuracy for Western names with initials, reducing manual overrides.Weaknesses:
Dr. vs. Prof. vs. さん).Van der Waals or O'Connor).assert or DTOs).Use Cases:
given_name, family_name).McDonald vs. MacDonald) may require manual review.Laravel-Specific Considerations:
config/name-parser.php).parse:name for bulk processing (e.g., php artisan parse:name --file=users.csv).required|name|min:2).data.attributes.givenName).Testing:
TJ Hooker → given_name: TJ, family_name: Hooker.J.R.R. Tolkien → should split "J.R.R." or treat as given_name?Иван Иванов → given_name: Иван, family_name: Иванов.| Risk Area | Mitigation Strategy |
|---|---|
| PHP Version Mismatch | Test on PHP 8.2+ with phpunit/phpunit@^10; patch if needed (e.g., str_contains deprecation). |
| Accuracy Gaps | Supplement with rule-based fallbacks (e.g., regex for common patterns like `Mc |
| Dependency Bloat | Audit for unused composer packages (e.g., phpunit if not needed). |
| Maintenance Rot | Fork the repo to backport fixes or contribute to a maintained alternative (e.g., Laravel Nameable). |
| Thread Safety | Stateless design → safe for queues (e.g., Laravel Horizon). |
| New Bugs | Re-test combined initials with: |
A.B. de la Cruz → given_name: A.B., family_name: de la Cruz?Van Dyke → should Van be treated as a prefix? |TJ Hooker fix sufficient, or do you need to handle more complex initials (e.g., III, IV)?users.name vs. users.given_name, users.family_name)?Laravel Ecosystem Synergy:
// config/name-parser.php
'rules' => [
'initials' => true, // Enable combined initials fix
'prefixes' => ['Mr.', 'Mrs.', 'Dr.'], // Customize recognized prefixes
],
name rule:
use TheIconic\NameParser\NameParser;
Validator::extend('name', function ($attribute, $value, $parameters, $validator) {
$parser = app(NameParser::class);
$result = $parser->parse($value);
return !empty($result['given_name']) && strlen($result['given_name']) >= 2;
});
NameParsed events for side effects (e.g., log parsing attempts):
event(new NameParsed($name, $parsedResult));
Testing facade to assert parsed outputs:
$this->assertEquals('TJ', $parser->parse('TJ Hooker')['given_name']);
Non-Laravel Dependencies:
Phase 1: Proof of Concept (1 week)
composer require theiconic/name-parser:^1.2.11.TJ Hooker, A.B. Smith, Van Dyke).Phase 2: Integration (2 weeks)
NameParserService facade/class with config support.UserObserver)./api/users/parse-name).required|name|min:2).Phase 3: Optimization (1 week)
Redis for parsed names).str_contains deprecation).mbstring improves multibyte support.name into given_name, family_name).| Priority | Task | Dependencies |
|---|---|---|
| 1 | Install and test parser in isolation (focus on combined initials fix). | None |
| 2 | Create NameParserService with config support. |
Parser installed |
| 3 | Integrate into user registration flow. | Service registered |
| 4 | Add validation rules to forms. | Service integrated |
| 5 | Build API endpoint for parsing. | Service + validation in place |
| 6 | Write tests for edge cases (initials, non-Latin, etc.). | All prior tasks |
| 7 | Optimize (caching, batch processing). | Testing complete |
How can I help you explore Laravel packages today?