The package provides:
StringHelper
that has static methods to work with strings;NumericHelper
that has static methods to work with numeric strings;Inflector
provides methods such astoPlural()
ortoSlug()
that derive a new string based on the string given;WildcardPattern
is a shell wildcard pattern to match strings against.
- PHP 7.4 or higher.
composer require yiisoft/strings --prefer-dist
String helper methods are static so usage is like the following:
echo \Yiisoft\Strings\StringHelper::countWords('Strings are cool!'); // 3
Overall the helper has the following method groups.
- byteLength
- byteSubstring
- baseName
- directoryName
- substring
- replaceSubstring
- startsWith
- startsWithIgnoringCase
- endsWith
- endsWithIgnoringCase
- truncateBegin
- truncateMiddle
- truncateEnd
- truncateWords
- length
- countWords
- lowercase
- uppercase
- uppercaseFirstCharacter
- uppercaseFirstCharacterInEachWord
- base64UrlEncode
- base64UrlDecode
- split
Numeric helper methods are static so usage is like the following:
echo \Yiisoft\Strings\NumericHelper::toOrdinal(3); // 3rd
The following methods are available:
- toOrdinal
- normalize
- isInteger
echo (new \Yiisoft\Strings\Inflector())->withoutIntl()->toSlug('Strings are cool!'); // strings-are-cool
Overall the inflector has the following method groups.
- toPlural
- toSingular
- toTransliterated
- pascalCaseToId
- toPascalCase
- toCamelCase
- toSentence
- toWords
- toHumanReadable
- classToTable
- tableToClass
- toSlug
WildcardPattern
allows a simple POSIX-style string matching.
use \Yiisoft\Strings\WildcardPattern;
$startsWithTest = new WildcardPattern('test*');
if ($startsWithTest->match('testIfThisIsTrue')) {
echo 'It starts with "test"!';
}
The following characters are special in the pattern:
\
escapes other special characters if usage of escape character is not turned off.*
matches any string, including the empty string, except delimiters (/
and\
by default).**
matches any string, including the empty string and delimiters.?
matches any single character.[seq]
matches any character in seq.[a-z]
matches any character from a to z.[!seq]
matches any character not in seq.[[:alnum:]]
matches POSIX style character classes.
ignoreCase()
could be called before doing a match()
to get a case-insensitive match:
use \Yiisoft\Strings\WildcardPattern;
$startsWithTest = new WildcardPattern('test*');
if ($startsWithTest->ignoreCase()->match('tEStIfThisIsTrue')) {
echo 'It starts with "test"!';
}
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
The package tests are checked with Infection mutation framework. To run it:
./vendor/bin/infection
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
The Yii Strings is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.