Susina Utils

StringUtils Class

Susina\Utils\StringUtils class provides utility static methods to manipulate strings, not included into the standard PHP functions list.

Why static methods? Starting from PHP 8.5, the language has introduced the new pipe operator, allowing chaining functions and methods in a more functional programming style. The static methods of this library are designed to be used with this new operator, enabling a more concise and expressive way to manipulate strings.

If you prefer the object-oriented fluent api approach, you can use the Text class, included into phootwork/lang package, or the UnicodeString class, from symfony/string library, instead.

toKebabCase method

toKebabCase method converts a string to kebab-case, which is a lowercase string with words separated by hyphens.

use Susina\Utils\StringUtils;

$kebabCase = StringUtils::toKebabCase('Hello World'); // hello-world
$kebabCase = StringUtils::toKebabCase('HelloWorld'); // hello-world
$kebabCase = StringUtils::toKebabCase('hello_world'); // hello-world
$kebabCase = StringUtils::toKebabCase('hello world'); // hello-world
$kebabCase = StringUtils::toKebabCase('helloWorld'); // hello-world

toSnakeCase method

toSnakeCase method converts a string to snake_case, which is a lowercase string with words separated by underscores.

use Susina\Utils\StringUtils;

$snakeCase = StringUtils::toSnakeCase('Hello World'); // hello_world
$snakeCase = StringUtils::toSnakeCase('HelloWorld'); // hello_world
$snakeCase = StringUtils::toSnakeCase('hello-world'); // hello_world
$snakeCase = StringUtils::toSnakeCase('hello world'); // hello_world
$snakeCase = StringUtils::toSnakeCase('helloWorld'); // hello_world

toStudlyCase method

toStudlyCase method converts a string to StudlyCase, which is a string with the first letter of each word capitalized and no spaces or separators.

use Susina\Utils\StringUtils;

$studlyCase = StringUtils::toStudlyCase('hello world'); // HelloWorld
$studlyCase = StringUtils::toStudlyCase('hello-world'); // HelloWorld
$studlyCase = StringUtils::toStudlyCase('hello_world'); // HelloWorld
$studlyCase = StringUtils::toStudlyCase('helloWorld'); // HelloWorld

toPascalCase method

Alias of toStudlyCase method.

toCamelCase method

toCamelCase method converts a string to camelCase, which is a string with the first letter of the first word in lowercase and the first letter of each subsequent word capitalized, with no spaces or separators.

use Susina\Utils\StringUtils;

$camelCase = StringUtils::toCamelCase('hello world'); // helloWorld
$camelCase = StringUtils::toCamelCase('hello-world'); // helloWorld
$camelCase = StringUtils::toCamelCase('hello_world'); // helloWorld
$camelCase = StringUtils::toCamelCase('HelloWorld'); // helloWorld

ensureStart method

ensureStart method ensures that a string starts with a specific character or string.

use Susina\Utils\StringUtils;

// Adds the leading slash
$var = StringUtils::ensureStart('my/personal/path', '/'); // /my/personal/path

// Does not add the leading slash, as it is already present
$var = StringUtils::ensureStart('/my/personal/path', '/'); // /my/personal/path

ensureEnd method

ensureEnd method ensures that a string ends with a specific character or string.

use Susina\Utils\StringUtils;

// Adds the trailing slash
$var = StringUtils::ensureEnd('my/personal/path', '/'); // my/personal/path/

// Does not add the trailing slash, as it is already present
$var = StringUtils::ensureEnd('my/personal/path/', '/'); // my/personal/path/

collapseSpaces method

collapseSpaces method collapses multiple spaces into a single space.

use Susina\Utils\StringUtils;

$var = StringUtils::collapseSpaces('  hello       world     '); // hello world

pluralize method

pluralize method converts a singular word to its plural form.

use Susina\Utils\StringUtils;

$var = StringUtils::pluralize('cat'); // cats
$var = StringUtils::pluralize('dog'); // dogs
$var = StringUtils::pluralize('child'); // children
$var = StringUtils::pluralize('person'); // people

singularize method

singularize method converts a plural word to its singular form.

use Susina\Utils\StringUtils;

$var = StringUtils::singularize('cats'); // cat
$var = StringUtils::singularize('dogs'); // dog
$var = StringUtils::singularize('children'); // child
$var = StringUtils::singularize('people'); // person

Both pluralize and singularize methods use Susina\Utils\Inflector\SimpleInflector by default. SimpleInflector is a standard English pluralizer/singularizer, if you need to use an inflector supporting other languages you can pass an instance of a class implementing Susina\Utils\Inflector\InflectorInterface methods as second argument. You can find some examples of how to use the Doctrine Inflector class with StringUtils in the How To section of this documentation.

Search results