A fast, solid and strong typed string manipulation library with multibyte support
A fast, solid and strong typed string manipulation library with multibyte support
$str = new Str('Hello, 世界');
$str->last(2); // 世界
$str->chars(); // ['世', '界']
$str
->ensureLeft('Hello, ') // Hello, 世界
->ensureRight('!!!') // Hello, 世界!!!
->trimRight('!') // Hello, 世界
->prepend('Str say - '); // Str say - Hello, 世界
$send = function (string $s) {};
$send((string)$str); // same
$send($str->getString()); // sameRequirements:
composer require str/strA fast string manipulation library with multi-byte support. Inspired by the "Stringy" library, with focus on speed.
Lib uses php7 features and does not throw any exceptions (because all input parameters are strongly typed). The code is completely covered by unit tests.
A
B
C
D
E
F
G
H
I
J
L
M
O
P
Q
R
S
T
U
W
Inserts given $substr $times into the original string after the first occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->afterFirst('a', 'duh', 2);
// foo baduhduhr bazParameters:
Return:
Inserts given $substr $times into the original string after the last occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->afterLast('a', 'duh', 2);
// foo bar baduhduhzParameters:
Return:
Append $sub to the string.
$str = new Str('/Acme');
echo (string)$str->append('/');
// /Acme/Parameters:
Return:
Appends a random string consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax to the original string.
$str = new Str('foo');
echo $str->appendUniqueIdentifier(3, -1, 'foba_rz');
// foozroParameters:
Return:
Returns the character at $pos, with indexes starting at 0.
$str = new Str('/Acme/');
echo (string)$str->at(2);
// cParameters:
Return:
Inserts given $substr $times into the original string before the first occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->beforeFirst('a', 'duh');
// foo bduhar bazParameters:
Return:
Inserts given $substr $times into the original string before the last occurrence of $needle.
$str = new Str('foo bar baz');
echo (string)$str->beforeLast('a', 'duh');
// foo bar bduhazParameters:
Return:
Returns the substring between $start and $end, if found, or an empty string. An optional $offset may be supplied from which to begin the search for the start string.
$str = new Str('/Acme/');
echo (string)$str->between('/', '/');
// AcmeParameters:
Return:
Returns a camelCase version of the string. Trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, as well as underscores.
$str = new Str('ac me');
echo (string)$str->camelize();
// acMeParameters: nothing
Return:
Returns an array consisting of the characters in the string.
$str = new Str('/Acme/');
echo (string)$str->chars();
// ['/', 'A', 'c', 'm', 'e', '/']Parameters: nothing
Return:
Cuts the original string in pieces of $step size.
$str = new Str('foo bar baz');
echo $str->chop(2);
// ['fo', 'o ', 'ba', 'r ', 'ba', 'z']Parameters:
Return:
Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multi-byte whitespace such as the thin space and ideographic space.
$str = new Str('foo bar baz');
echo (string)$str->collapseWhitespace();
// foo bar bazParameters: nothing
Return:
Check if the string contains $needle substring.
$str = new Str('/Acme/');
echo $str->contains('/');
// true
$str = new Str('/Acme/');
echo $str->contains('a', false);
// trueParameters:
Return:
Returns true if the string contains all $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->containsAll(['m', 'c', '/']);
// trueParameters:
Return:
Returns true if the string contains any $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->containsAny(['foo', 'c', 'bar']);
// trueParameters:
Return:
Returns the number of occurrences of $needle in the given string. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->countSubstr('m');
// 2Parameters:
Return:
Returns a lowercase and trimmed string separated by dashes. Dashes are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as underscores.
$str = new Str('Ac me');
echo (string)$str->dasherize();
// ac-meParameters: nothing
Return:
Returns a lowercase and trimmed string separated by the given $delimiter. Delimiters are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces, dashes, and underscores. Alpha delimiters are not converted to lowercase.
$str = new Str('Ac me');
echo (string)$str->delimit('#');
// ac#meParameters:
Return:
Returns true if the string ends with $substring, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->endsWith('e/');
// trueParameters:
Return:
Returns true if the string ends with any of $substrings, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = new Str('/Accmme/');
echo $str->endsWithAny(['foo', 'e/', 'bar']);
// trueParameters:
Return:
Check whether $prefix exists in the string, and prepend $prefix to the string if it doesn't.
$str = new Str('Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/
$str = new Str('/Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/Parameters:
Return:
Check whether $suffix exists in the string, and append $suffix to the string if it doesn't.
$str = new Str('/Acme');
echo (string)$str->ensureRight('/'); // /Acme/
$str = new Str('/Acme/');
echo (string)$str->ensureRight('/'); // /Acme/Parameters:
Return:
Returns the first $length characters of the string.
$str = new Str('/Acme/');
echo (string)$str->first(2);
// /AParameters:
Return:
Parameters: nothing
Return:
Returns true if the string contains a lower case char, false otherwise.
$str = new Str('Acme');
echo $str->hasLowerCase();
// trueParameters: nothing
Return:
Check if the string has $prefix at the start.
$str = new Str('/Acme/');
echo $str->hasPrefix('/');
// trueParameters:
Return:
Check if the string has $suffix at the end.
$str = new Str('/Acme/');
echo $str->hasSuffix('/');
// trueParameters:
Return:
Returns true if the string contains an upper case char, false otherwise.
$str = new Str('Acme');
echo $str->hasUpperCase();
// trueParameters: nothing
Return:
Convert all HTML entities to their applicable characters. An alias of html_entity_decode. For a list of flags, refer to PHP documentation.
$str = new Str('<Acme>');
echo (string)$str->htmlDecode();
// Parameters:
Return:
Convert all applicable characters to HTML entities. An alias of htmlentities. Refer to PHP documentation for a list of flags.
$str = new Str('');
echo (string)$str->htmlEncode();
// <Acme>Parameters:
Return:
Capitalizes the first word of the string, replaces underscores with spaces.
$str = new Str('foo_id');
echo (string)$str->humanize();
// FooParameters: nothing
Return:
Returns the index of the first occurrence of $needle in the string, and -1 if not found. Accepts an optional $offset from which to begin the search.
$str = new Str('/Accmme/');
echo $str->indexOf('m');
// 4Parameters:
Return:
Returns the index of the last occurrence of $needle in the string, and false if not found. Accepts an optional $offset from which to begin the search. Offsets may be negative to count from the last character in the string.
$str = new Str('/Accmme/');
echo $str->indexOfLast('No open issues yet, or sync has not completed.