lukaswhite/capitalizer

A simple library for capitalizing strings intelligently.

0.0.1 2021-09-18 06:38 UTC

This package is auto-updated.

Last update: 2024-10-18 13:49:34 UTC


README

A package for intelligent string capitalization.

PHP includes the ucwords function for capitalizing words in strings, but it's pretty dumb.

Consider the following names:

Nigel de Jong
Rajiv van La Parra
Malcolm McDonald

...or the following titles:

To Be or Not to Be
The Wizard of Oz
Gone With the Wind

...or the following places:

Newton on the Willow
Stoke on Trent
Stoke D'Abernon

This library is designed to properly handle cases like this.

Usage

Create an instance with no arguments:

use Lukaswhite\Capitalizer\Capitalizer;
$capitalizer = new Capitalizer();

The method provides four methods for capitalization:

title()

e.g. the title of a book, film, etc.

print $capitalizer->title('to be or not to be'); // prints To Be or Not to Be

name()

The name of a person

print $capitalizer->name('RAJIV VAN LA PARRA'); // prints Rajiv van La Parra

place()

The name of a place.

print $capitalizer->place('STOKE ON TRENT'); // prints Stoke on Trent

string()

A generic string

print $capitalizer->title('this is a string); // prints This is a String

Customizing Behaviour

You can customise the behaviour of the library using one or more of the following methods:

$capitalizer = new Capitalizer();

$capitalizer->addLowercase('con'); // e.g. Chilli con Carne

$capitalizer->addUppercase('php'); // good for acronyms

$capitalizer->addLowercaseName('of'); // only applies to names; e.g. Jesus of Nazareth

$capitalizer->addPrefix('Mc');

$capitalizer->addSuffix('\'s');

Note that all of them return the current instance (i.e. a fluent interface), so they can be chained together, for example:

$capitalizer = new Capitalizer();

$capitalizer->addLowercase('con')
    ->addUppercase('php')
    ->addLowercaseName('of')
    ->addPrefix('Mc')
    ->addSuffix('\'s');

Refer to the source code for more explanation of these methods.