A simple library for capitalizing strings intelligently.

0.0.1 2021-09-18 06:38 UTC

This package is auto-updated.

Last update: 2024-02-18 12:22:57 UTC


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.


Create an instance with no arguments:

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

The method provides four methods for capitalization:


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


The name of a person

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


The name of a place.

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


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



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();


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