Giter Club home page Giter Club logo

name-parser's Introduction

THE ICONIC Name Parser

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License

Purpose

This is a universal, language-independent name parser.

Its purpose is to split a single string containing a full name, possibly including salutation, initials, suffixes etc., into meaningful parts like firstname, lastname, initials, and so on.

It is mostly tailored towards english names but works pretty well with non-english names as long as they use latin spelling.

E.g. Mr Anthony R Von Fange III is parsed to

  • salutation: Mr.
  • firstname: Anthony
  • initials: R
  • lastname: von Fange
  • suffix: III

Features

Supported patterns

This parser is able to handle name patterns with and without comma:

... [firstname] ... [lastname] ...
... [lastname] ..., ... [firstname] ...
... [lastname] ..., ... [firstname] ..., [suffix]

Supported parts

  • salutations (e.g. Mr, Mrs, Dr, etc.)
  • first name
  • middle names
  • initials (single letters, possibly followed by a dot)
  • nicknames (parts within parenthesis, brackets etc.)
  • last names (also supports prefixes like von, de etc.)
  • suffixes (Jr, Senior, 3rd, PhD, etc.)

Other features

  • multi-language support for salutations, suffixes and lastname prefixes
  • customizable nickname delimiters
  • customizable normalisation of all output strings (original values remain accessible)
  • customizable whitespace

Examples

More than 80 different successfully parsed name patterns can be found in the parser unit test.

Setup

composer require CurioLabs/name-parser

Usage

Basic usage

<?php

$parser = new CurioLabs\NameParser\Parser();

$name = $parser->parse($input);

echo $name->getSalutation();
echo $name->getFirstname();
echo $name->getLastname();
echo $name->getMiddlename();
echo $name->getNickname();
echo $name->getInitials();
echo $name->getSuffix();

print_r($name->getAll()); // all parts as an associative array

echo $name; // re-prints the full normalised name

An empty string is returned for missing parts.

Special part retrieval features

Explicit last name parts

You can retrieve last name prefixes and pure last names separately with

echo $name->getLastnamePrefix();
echo $name->getLastname(true); // true enables strict mode for pure lastnames, only

Nick names with normalized wrapping

By default, getNickname() returns the pure string of nick names. However, you can pass true to have the same normalised parenthesis wrapping applied as in echo $name:

echo $name->getNickname(); // The Giant
echo $name->getNickname(true); // (The Giant)

Setting Languages

$parser = new CurioLabs\NameParser\Parser([
    new CurioLabs\NameParser\Language\English(), //default
    new CurioLabs\NameParser\Language\German(),
])

Setting nickname delimiters

$parser = new CurioLabs\NameParser\Parser();
$parser->setNicknameDelimiters(['(' => ')']);

Setting whitespace characters

$parser = new CurioLabs\NameParser\Parser();
$parser->setWhitespace("\t _.");

Limiting the position of salutations

$parser = new CurioLabs\NameParser\Parser();
$parser->setMaxSalutationIndex(2);

This will require salutations to appear within the first two words of the given input string. This defaults to half the amount of words in the input string, meaning that effectively the salutation may occur within the first half of the name parts.

License

THE ICONIC Name Parser library for PHP is released under the MIT License.

name-parser's People

Contributors

codeduck42 avatar curiolabs avatar estringana avatar francislavoie avatar wyrfel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.