Yii - String Helper

1.0.0 2020-08-31 20:56 UTC

This package is auto-updated.

Last update: 2020-09-25 12:01:52 UTC



Yii Strings

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis

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 as toPlural() or toSlug() that derive a new string based on the string given;
  • WildcardPattern is a shell wildcard pattern to match strings against.


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.


  • byteLength
  • byteSubstring

File paths

  • baseName
  • directoryName


  • substring
  • replaceSubstring
  • startsWith
  • startsWithIgnoringCase
  • endsWith
  • endsWithIgnoringCase


  • truncateBegin
  • truncateMiddle
  • truncateEnd
  • truncateWords


  • length
  • countWords

Lowercase and uppercase

  • lowercase
  • uppercase
  • uppercaseFirstCharacter
  • uppercaseFirstCharacterInEachWord

URL friendly base64

  • base64UrlEncode
  • base64UrlDecode

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

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


  • toTransliterated

Case conversion

  • pascalCaseToId
  • toPascalCase
  • toCamelCase

Words and sentences

  • toSentence
  • toWords
  • toHumanReadable

Classes and database tables

  • classToTable
  • tableToClass


  • 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.

When matching file paths, it is advised to use both withExactSlashes() and withExactLeadingPeriod():

use \Yiisoft\Strings\WildcardPattern;

$startsWithTest = (new WildcardPattern('config/*.php'))

if ($startsWithTest->match($fileName)) {
    echo 'It is a config!';