\DewdropInflector

Some utility functions that make it easy to convert from one text format to another (e.g. from CamelCase to under_scores).

This tool is used in various places to created default human-friendly titles from database table or column names, to convert class names to paths, etc.

This is more or less a direct port of the PEAR Text_Inflector package.

Summary

Methods
Properties
Constants
pluralize()
singularize()
conditionalPlural()
listWithConjunction()
titleize()
camelize()
underscore()
hyphenize()
humanize()
variablize()
tableize()
classify()
ordinalize()
unaccent()
urlize()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

pluralize()

pluralize(string  $word) : string

Pluralizes English nouns.

Parameters

string $word

English noun to pluralize

Returns

string —

Plural noun

singularize()

singularize(string  $word) : string

Singularizes English nouns.

Parameters

string $word

English noun to singularize

Returns

string —

Singular noun.

conditionalPlural()

conditionalPlural(integer  $number, string  $word) : string

Get the plural form of a word if first parameter is greater than 1

Parameters

integer $number
string $word

Returns

string —

Pluralized string when number of items is greater than 1

listWithConjunction()

listWithConjunction(array  $items, string  $conjunction = 'and') : string

Generate a list, separated by commas and the specified conjunction, depending on the number of items provided.

Parameters

array $items
string $conjunction

Returns

string

titleize()

titleize(string  $word, string  $uppercase = '') : string

Converts an underscored or CamelCase word into a English sentence.

The titleize function converts text like "WelcomePage", "welcome_page" or "welcome page" to this "Welcome Page". If second parameter is set to 'first' it will only capitalize the first character of the title.

Parameters

string $word

Word to format as tile

string $uppercase

If set to 'first' it will only uppercase the first character. Otherwise it will uppercase all the words in the title.

Returns

string —

Text formatted as title

camelize()

camelize(string  $word) : string

Returns given word as CamelCased

Converts a word like "send_email" to "SendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "WhoSOnline"

Parameters

string $word

Word to convert to camel case

Returns

string —

UpperCamelCasedWord

underscore()

underscore(string  $word) : string

Converts a word "into_it_s_underscored_version"

Convert any "CamelCased" or "ordinary Word" into an "underscored_word".

This can be really useful for creating friendly URLs.

Parameters

string $word

Word to underscore

Returns

string —

Underscored word

hyphenize()

hyphenize(string  $word) : string

Just like underscore(), but with hyphens.

Parameters

string $word

Returns

string

humanize()

humanize(string  $word, string  $uppercase = '') : string

Returns a human-readable string from $word

Returns a human-readable string from $word, by replacing underscores with a space, and by upper-casing the initial character by default.

If you need to uppercase all the words you just have to pass 'all' as a second parameter.

Parameters

string $word

String to "humanize"

string $uppercase

If set to 'all' it will uppercase all the words instead of just the first one.

Returns

string —

Human-readable word

variablize()

variablize(string  $word) : string

Same as camelize but first char is lowercased

Converts a word like "send_email" to "sendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "whoSOnline"

Parameters

string $word

Word to lowerCamelCase

Returns

string —

Returns a lowerCamelCasedWord

tableize()

tableize(string  $className) : string

Converts a class name to its table name according to rails naming conventions.

Converts "Person" to "people"

Parameters

string $className

Class name for getting related table_name.

Returns

string

classify()

classify(string  $tableName) : string

Converts a table name to its class name according to Rails naming conventions.

Converts "people" to "Person"

Parameters

string $tableName

Table name for getting related ClassName.

Returns

string

ordinalize()

ordinalize(integer  $number) : string

Converts number to its ordinal English form.

This method converts 13 to 13th, 2 to 2nd ...

Parameters

integer $number

Number to get its ordinal value

Returns

string —

Ordinal representation of given string.

unaccent()

unaccent(string  $text) : string

Transforms a string to its unaccented version.

This might be useful for generating "friendly" URLs.

Parameters

string $text

Returns

string

urlize()

urlize(string  $text) : string

Convert the supplied text to a format friendly for URL segments.

Parameters

string $text

Returns

string