A simple class to correctly capitalize full names.
Require this package with Composer in the root directory of your project
composer require enricodias/nameize
and include the composer's autoloader in your code
include 'vendor/autoload.php';
echo \enricodias\Nameize::create()->name("Carlo D'ippoliti"); // Carlo D'Ippoliti
or
$nameize = new \enricodias\Nameize();
echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->name("john o'grady-smith"); // John O'Grady-Smith
The method setAllowedCharacters()
receives an array of special characters. Those characters signalizes that the next letter should be in upper case. If no character is specified, the default array("'", '-')
is used. If you pass a string, it will be consider a single character.
$nameize = new \enricodias\Nameize();
$nameize->setAllowedCharacters("'");
echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->name("john o'grady-smith"); // John O'Grady-smith
or with method chaining:
echo \enricodias\Nameize::create()
->setAllowedCharacters("-")
->name("john o'grady-smith"); // John O'grady-Smith
Some languages require capitalization on the first letter of every word regardless of their size. The setMinLength()
method sets the minimum length of which words will be capitalized (min: 1, max: 5, default: 4).
$nameize = new \enricodias\Nameize();
$nameize->setMinLength(1);
echo $nameize->name("Tri vu phu"); // Tri Vu Phu
echo $nameize->name("Shuanping dai"); // Shuanping Dai
or with method chaining:
echo \enricodias\Nameize::create()
->setMinLength(1)
->name("Tri vu phu"); // Tri Vu Phu
Your application may detect the user's country and use the appropriate minLength value.
If you need more features I recommend using a name parser such as https://github.com/theiconic/name-parser