yiisoft / strings
Yii - String Helper
Fund package maintenance!
Open Collective
yiisoft
Installs: 96 161
Dependents: 34
Suggesters: 0
Security: 0
Stars: 29
Watchers: 18
Forks: 11
Open Issues: 3
Requires
- php: ^7.4|^8.0
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^9.4
- roave/infection-static-analysis-plugin: ^1.3
- vimeo/psalm: ^4.1
This package is auto-updated.
Last update: 2021-01-11 09:17:07 UTC
README
Yii Strings
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.
Installation
composer require yiisoft/strings
StringHelper usage
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.
Bytes
- byteLength
- byteSubstring
File paths
- baseName
- directoryName
Substrings
- substring
- replaceSubstring
- startsWith
- startsWithIgnoringCase
- endsWith
- endsWithIgnoringCase
Truncation
- truncateBegin
- truncateMiddle
- truncateEnd
- truncateWords
Counting
- length
- countWords
Lowercase and uppercase
- lowercase
- uppercase
- uppercaseFirstCharacter
- uppercaseFirstCharacterInEachWord
URL friendly base64
- base64UrlEncode
- base64UrlDecode
Other
- split
NumericHelper usage
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
Inflector usage
echo (new \Yiisoft\Strings\Inflector())->withoutIntl()->toSlug('Strings are cool!'); // strings-are-cool
Overall the inflector has the following method groups.
Plurals and singulars
- toPlural
- toSingular
Transliteration
- toTransliterated
Case conversion
- pascalCaseToId
- toPascalCase
- toCamelCase
Words and sentences
- toSentence
- toWords
- toHumanReadable
Classes and database tables
- classToTable
- tableToClass
URLs
- toSlug
WildcardPattern usage
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.?
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.
Several options are available. Call these before doing a match()
:
withoutEscape()
- makes\
a regular character in a pattern.withExactSlashes()
- makes\
in a string to match\
only in a pattern.ignoreCase()
- case-insensitive match.withExactLeadingPeriod()
- makes first.
in a string match only.
in a pattern.withEnding()
- match ending of testing string.
When matching file paths, it is advised to use both withExactSlashes()
and withExactLeadingPeriod()
:
use \Yiisoft\Strings\WildcardPattern; $startsWithTest = (new WildcardPattern('config/*.php')) ->withExactLeadingPeriod() ->withExactSlashes(); if ($startsWithTest->match($fileName)) { echo 'It is a config!'; }
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework. To run it:
./vendor/bin/infection
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
Support the project
Follow updates
License
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.