StringUtils
in package
Utility methods to manipulate strings, not included into the standard PHP functions list.
This class contains static methods to manipulate strings, such as converting to kebab-case, snake_case, StudlyCase, and camelCase. It also provides methods to ensure that a string starts or ends with a specific character.
All the static methods are intended to be used with the new PHP 8.5 pipe operator, allowing for a more functional programming style.
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 package, instead.
Tags
Table of Contents
Methods
- collapseSpaces() : string
- Collapses multiple consecutive whitespace characters into a single space.
- ensureEnd() : string
- Ensure that the `$root` string ends with `$char` character/string.
- ensureStart() : string
- Ensure that the `$root` string starts with `$char` character/string.
- toCamelCase() : string
- Converts this string into camelCase. Numbers are considered as part of its previous piece.
- toKebabCase() : string
- Convert this string into kebab-case. Numbers are considered as part of its previous piece.
- toPascalCase() : string
- Alias for toStudlyCase() method.
- toSnakeCase() : string
- Converts this string into snake_case. Numbers are considered as part of its previous piece.
- toStudlyCase() : string
- Converts this string into StudlyCase. Numbers are considered as part of its previous piece.
Methods
collapseSpaces()
Collapses multiple consecutive whitespace characters into a single space.
public
static collapseSpaces(string $toCollapse) : string
Parameters
- $toCollapse : string
-
The string to collapse.
Tags
Return values
string —The collapsed string.
ensureEnd()
Ensure that the `$root` string ends with `$char` character/string.
public
static ensureEnd(string $root, string $char) : string
In example:
<?php
use Susina\Utils\StringUtils;
$var = StringUtils::ensureEnd('my/personal/path', '/'); // my/personal/path/ added the trailing slash
$var = StringUtils::ensureEnd('my/personal/path/', '/'); // my/personal/path/ no change, already ends with slash
Parameters
- $root : string
-
The string to manipulate.
- $char : string
-
The char the given string should end with
Tags
Return values
string —The string ending with the given character.
ensureStart()
Ensure that the `$root` string starts with `$char` character/string.
public
static ensureStart(string $root, string $char) : string
In example:
<?php
use Susina\Utils\StringUtils;
$var = StringUtils::ensureStart('my/personal/path', '/'); // /my/personal/path added the leading slash
$var = StringUtils::ensureStart('/my/personal/path', '/'); // /my/personal/path no change, already starts with slash
Parameters
- $root : string
-
The string to manipulate.
- $char : string
-
The char the given string should start with
Tags
Return values
string —The string starting with the given character.
toCamelCase()
Converts this string into camelCase. Numbers are considered as part of its previous piece.
public
static toCamelCase(string $text) : string
In example:
<?php
use Susina\Utils\StringUtils;
$var = StringUtils::toCamelCase('my_own_variable'); // myOwnVariable
$var = StringUtils::toCamelCase('my_test3_variable'); // myTest3Variable
Parameters
- $text : string
Tags
Return values
stringtoKebabCase()
Convert this string into kebab-case. Numbers are considered as part of its previous piece.
public
static toKebabCase(string $text) : string
In example:
<?php
use Susina\Utils\StringUtils;
$var = StringUtils::toKebabCase('myOwnVariable'); // my-own-variable
$var = StringUtils::toKebabCase('myTest3Variable'); // my-test3-variable
Parameters
- $text : string
Tags
Return values
stringtoPascalCase()
Alias for toStudlyCase() method.
public
static toPascalCase(string $text) : string
StudlyCase is also known as PascalCase, and this method is provided for convenience.
Parameters
- $text : string
Tags
Return values
stringtoSnakeCase()
Converts this string into snake_case. Numbers are considered as part of its previous piece.
public
static toSnakeCase(string $text) : string
In example:
<?php
use Susina\Utils\StringUtils;
$var = StringUtils::toSnakeCase('myOwnVariable'); // my_own_variable
$var = StringUtils::toSnakeCase('myTest3Variable'); // my_test3_variable
Parameters
- $text : string
Tags
Return values
stringtoStudlyCase()
Converts this string into StudlyCase. Numbers are considered as part of its previous piece.
public
static toStudlyCase(string $text) : string
In example:
<?php
use Susina\Utils\StringUtils;
$var = StringUtils::toStudlyCase('my_own_variable'); // MyOwnVariable
$var = StringUtils::toStudlyCase('my_test3_variable'); // MyTest3Variable
Parameters
- $text : string