joshfraser/php-name-parser
Composer 安装命令:
composer require joshfraser/php-name-parser
包简介
PHP library to split names into their respective components (first, last, etc)
README 文档
README
PHP library to split names into their respective components. Besides detecting first and last names, this library attempts to handle prefixes, suffixes, initials and compound last names like "Von Fange". It also normalizes prefixes (Mister -> Mr.) and fixes capitalization (JOHN SMITH -> John Smith).
Usage:
include("parser.php");
$parser = new FullNameParser();
$parser->parse_name("Mr Anthony R Von Fange III");
Results:
Array (
[nickname] =>
[salutation] => Mr.
[fname] => Anthony
[initials] => R
[lname] => Von Fange
[suffix] => III
)
The algorithm:
We start by splitting the full name into separate words. We then do a dictionary lookup on the first and last words to see if they are a common prefix or suffix. Next, we take the middle portion of the string (everything minus the prefix & suffix) and look at everything except the last word of that string. We then loop through each of those words concatenating them together to make up the first name. While we’re doing that, we watch for any indication of a compound last name. It turns out that almost every compound last name starts with 1 of 16 prefixes (Von, Van, Vere, etc). If we see one of those prefixes, we break out of the first name loop and move on to concatenating the last name. We handle the capitalization issue by checking for camel-case before uppercasing the first letter of each word and lowercasing everything else. I wrote special cases for periods and dashes. We also have a couple other special cases, like ignoring words in parentheses all-together.
Check examples.php for the test suite and examples of how various name formats are parsed.
Possible improvements
- Handle the "Lname, Fname" format
- Separate the parsing of the name from the normalization & capitalization & make those optional
- Seperate the dictionaries from the code to make it easier to do localization
- Add common name libraries to allow for things like gender detection
Same logic, different languages
Credits & license:
- Read more about the inspiration for this PHP Name Parser library by Josh Fraser
- Special thanks to Josh Jones, Timothy Wood, Michael Waskosky, Eric Celeste, Josh Houghtelin and monitaure for their contributions. Pull requests are always welcome as long as you don't break the test suite.
- Released under Apache 2.0 license
joshfraser/php-name-parser 适用场景与选型建议
joshfraser/php-name-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.06M 次下载、GitHub Stars 达 189, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 joshfraser/php-name-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 joshfraser/php-name-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.06M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 190
- 点击次数: 26
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2026-01-04