alignwebs/php-name-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alignwebs/php-name-parser

Composer 安装命令:

composer require alignwebs/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).

Installation:

composer require alignwebs/php-name-parser

Usage:

use FullNameParser\FullNameParser;

$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:

统计信息

  • 总下载量: 2.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 64
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-01-14