The package provides:
StringHelperthat has static methods to work with strings;NumericHelperthat has static methods to work with numeric strings;Inflectorprovides methods such astoPlural()ortoSlug()that derive a new string based on the string given;WildcardPatternis a shell wildcard pattern to match strings against;CombinedRegexpis a wrapper that optimizes multiple regular expressions matching andMemoizedCombinedRegexpis a decorator that caches results ofCombinedRegexpto speed up matching.
- PHP 8.1 or higher.
mbstringPHP extension.
The package could be installed with Composer:
composer require yiisoft/stringsString helper methods are static so usage is like the following:
echo \Yiisoft\Strings\StringHelper::countWords('Strings are cool!'); // 3Overall the helper has the following method groups.
- byteLength
- byteSubstring
- baseName
- directoryName
- substring
- replaceSubstring
- startsWith
- startsWithIgnoringCase
- endsWith
- endsWithIgnoringCase
- findBetween
- findBetweenFirst
- findBetweenLast
- truncateBegin
- truncateMiddle
- truncateEnd
- truncateWords
- truncateWordsByLength
- trim
- ltrim
- rtrim
- length
- countWords
- lowercase
- uppercase
- uppercaseFirstCharacter
- uppercaseFirstCharacterInEachWord
- base64UrlEncode
- base64UrlDecode
- matchAnyRegex
- parsePath
- split
Numeric helper methods are static so usage is like the following:
echo \Yiisoft\Strings\NumericHelper::toOrdinal(3); // 3rdThe following methods are available:
- toOrdinal
- normalize
- isInteger
echo (new \Yiisoft\Strings\Inflector())
->withoutIntl()
->toSlug('Strings are cool!'); // strings-are-coolOverall 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"!';
}CombinedRegexp optimizes matching multiple regular expressions.
use \Yiisoft\Strings\CombinedRegexp;
$patterns = [
'first',
'second',
'^a\d$',
];
$regexp = new CombinedRegexp($patterns, 'i');
$regexp->matches('a5'); // true – matches the third pattern
$regexp->matches('A5'); // true – matches the third pattern because of `i` flag that is applied to all regular expressions
$regexp->getMatchingPattern('a5'); // '^a\d$' – the pattern that matched
$regexp->getMatchingPatternPosition('a5'); // 2 – the index of the pattern in the array
$regexp->getCompiledPattern(); // '~(?|first|second()|^a\d$()())~'MemoizedCombinedRegexp caches results of CombinedRegexp in memory.
It is useful when the same incoming string are matching multiple times or different methods of CombinedRegexp are called.
use \Yiisoft\Strings\CombinedRegexp;
use \Yiisoft\Strings\MemoizedCombinedRegexp;
$patterns = [
'first',
'second',
'^a\d$',
];
$regexp = new MemoizedCombinedRegexp(new CombinedRegexp($patterns, 'i'));
$regexp->matches('a5'); // Fires `preg_match` inside the `CombinedRegexp`.
$regexp->matches('first'); // Fires `preg_match` inside the `CombinedRegexp`.
$regexp->matches('a5'); // Does not fire `preg_match` inside the `CombinedRegexp` because the result is cached.
$regexp->getMatchingPattern('a5'); // The result is cached so no `preg_match` is fired.
$regexp->getMatchingPatternPosition('a5'); // The result is cached so no `preg_match` is fired.
// The following code fires only once matching mechanism.
if ($regexp->matches('second')) {
echo sprintf(
'Matched the pattern "%s" which is on the position "%s" in the expressions list.',
$regexp->getMatchingPattern('second'),
$regexp->getMatchingPatternPosition('second'),
);
}If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
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.