$str = newStr('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:
- php7.1
composer require str/str- strongly typed
- no exceptions thrown
- fast
- new functions
A 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
- indexOf
- indexOfLast
- insert
- isAlpha
- isAlphanumeric
- isBase64
- isBlank
- isEmail
- isHexadecimal
- isIpV4
- isIpV6
- isJson
- isLowerCase
- isSerialized
- isUUIDv4
- isUpperCase
J
L
M
O
P
Q
R
S
- safeTruncate
- shift
- shiftReversed
- shuffle
- slice
- slugify
- snakeize
- split
- startsWith
- startsWithAny
- stripWhitespace
- substr
- surround
- swapCase
T
- tidy
- titleize
- toAscii
- toBoolean
- toLowerCase
- toSpaces
- toTabs
- toTitleCase
- toUpperCase
- trim
- trimLeft
- trimRight
- truncate
U
W
Inserts given $substr $times into the original string after the first occurrence of $needle.
$str = newStr('foo bar baz');
echo (string)$str->afterFirst('a', 'duh', 2);
// foo baduhduhr bazParameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
Inserts given $substr $times into the original string after the last occurrence of $needle.
$str = newStr('foo bar baz');
echo (string)$str->afterLast('a', 'duh', 2);
// foo bar baduhduhzParameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
Append $sub to the string.
$str = newStr('/Acme');
echo (string)$str->append('/');
// /Acme/Parameters:
- string $sub
Return:
- \Str
Appends a random string consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax to the original string.
$str = newStr('foo');
echo$str->appendUniqueIdentifier(3, -1, 'foba_rz');
// foozroParameters:
- int $size
- int $sizeMax
- string $possibleChars
Return:
- \Str
Returns the character at $pos, with indexes starting at 0.
$str = newStr('/Acme/');
echo (string)$str->at(2);
// cParameters:
- int $pos
Return:
- \Str
Inserts given $substr $times into the original string before the first occurrence of $needle.
$str = newStr('foo bar baz');
echo (string)$str->beforeFirst('a', 'duh');
// foo bduhar bazParameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
Inserts given $substr $times into the original string before the last occurrence of $needle.
$str = newStr('foo bar baz');
echo (string)$str->beforeLast('a', 'duh');
// foo bar bduhazParameters:
- string $needle
- string $substr
- int $times
Return:
- \Str
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 = newStr('/Acme/');
echo (string)$str->between('/', '/');
// AcmeParameters:
- string $start
- string $end
- int $offset
Return:
- \Str
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 = newStr('ac me');
echo (string)$str->camelize();
// acMeParameters:nothing
Return:
- \Str
Returns an array consisting of the characters in the string.
$str = newStr('/Acme/');
echo (string)$str->chars();
// ['/', 'A', 'c', 'm', 'e', '/']Parameters:nothing
Return:
- array
Cuts the original string in pieces of $step size.
$str = newStr('foo bar baz');
echo$str->chop(2);
// ['fo', 'o ', 'ba', 'r ', 'ba', 'z']Parameters:
- int $step
Return:
- array
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 = newStr('foo bar baz');
echo (string)$str->collapseWhitespace();
// foo bar bazParameters:nothing
Return:
- \Str
Check if the string contains $needle substring.
$str = newStr('/Acme/');
echo$str->contains('/');
// true$str = newStr('/Acme/');
echo$str->contains('a', false);
// trueParameters:
- string $needle
- bool $caseSensitive
Return:
- bool
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 = newStr('/Accmme/');
echo$str->containsAll(['m', 'c', '/']);
// trueParameters:
- array $needles
- bool $caseSensitive
Return:
- bool
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 = newStr('/Accmme/');
echo$str->containsAny(['foo', 'c', 'bar']);
// trueParameters:
- array $needles
- bool $caseSensitive
Return:
- bool
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 = newStr('/Accmme/');
echo$str->countSubstr('m');
// 2Parameters:
- string $needle
- bool $caseSensitive
Return:
- int
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 = newStr('Ac me');
echo (string)$str->dasherize();
// ac-meParameters:nothing
Return:
- \Str
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 = newStr('Ac me');
echo (string)$str->delimit('#');
// ac#meParameters:
- $delimiter
Return:
- \Str
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 = newStr('/Accmme/');
echo$str->endsWith('e/');
// trueParameters:
- string $substring
- bool $caseSensitive
Return:
- bool
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 = newStr('/Accmme/');
echo$str->endsWithAny(['foo', 'e/', 'bar']);
// trueParameters:
- array $substrings
- bool $caseSensitive
Return:
- bool
Check whether $prefix exists in the string, and prepend $prefix to the string if it doesn't.
$str = newStr('Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/$str = newStr('/Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/Parameters:
- string $check
Return:
- \Str
Check whether $suffix exists in the string, and append $suffix to the string if it doesn't.
$str = newStr('/Acme');
echo (string)$str->ensureRight('/'); // /Acme/$str = newStr('/Acme/');
echo (string)$str->ensureRight('/'); // /Acme/Parameters:
- string $check
Return:
- \Str
Returns the first $length characters of the string.
$str = newStr('/Acme/');
echo (string)$str->first(2);
// /AParameters:
- int $length
Return:
- \Str
Parameters:nothing
Return:
- string
Returns true if the string contains a lower case char, false otherwise.
$str = newStr('Acme');
echo$str->hasLowerCase();
// trueParameters:nothing
Return:
- bool
Check if the string has $prefix at the start.
$str = newStr('/Acme/');
echo$str->hasPrefix('/');
// trueParameters:
- string $prefix
Return:
- bool
Check if the string has $suffix at the end.
$str = newStr('/Acme/');
echo$str->hasSuffix('/');
// trueParameters:
- string $suffix
Return:
- bool
Returns true if the string contains an upper case char, false otherwise.
$str = newStr('Acme');
echo$str->hasUpperCase();
// trueParameters:nothing
Return:
- bool
Convert all HTML entities to their applicable characters. An alias of html_entity_decode. For a list of flags, refer to PHP documentation.
$str = newStr('<Acme>');
echo (string)$str->htmlDecode();
// <Acme>Parameters:
- int $flags
Return:
- \Str
Convert all applicable characters to HTML entities. An alias of htmlentities. Refer to PHP documentation for a list of flags.
$str = newStr('<Acme>');
echo (string)$str->htmlEncode();
// <Acme>Parameters:
- int $flags
Return:
- \Str
Capitalizes the first word of the string, replaces underscores with spaces.
$str = newStr('foo_id');
echo (string)$str->humanize();
// FooParameters:nothing
Return:
- \Str
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 = newStr('/Accmme/');
echo$str->indexOf('m');
// 4Parameters:
- string $needle
- int $offset
Return:
- int
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 = newStr('/Accmme/');
echo$str->indexOfLast('m');
// 5Parameters:
- string $needle
- int $offset
Return:
- int
Inserts $substring into the string at the $index provided.
$str = newStr('/Ace/');
echo (string)$str->insert('m', 3);
// /Acme/Parameters:
- string $substring
- int $index
Return:
- \Str
Returns true if the string contains only alphabetic chars, false otherwise.
$str = newStr('Acme');
echo$str->isAlpha();
// trueParameters:nothing
Return:
- bool
Returns true if the string contains only alphabetic and numeric chars, false otherwise.
$str = newStr('Acme1');
echo$str->isAlphanumeric();
// trueParameters:nothing
Return:
- bool
Check if this string is valid base64 encoded data. Function do encode(decode(s)) === s, so this is not so fast.
Parameters:nothing
Return:
- bool
Returns true if the string contains only whitespace chars, false otherwise.
$str = newStr('Acme');
echo$str->isBlank();
// falseParameters:nothing
Return:
- bool
Splits the original string in pieces by '@' delimiter and returns true in case the resulting array consists of 2 parts.
$str = newStr('test@test@example.com');
echo$str->isEmail();
// falseParameters:nothing
Return:
- bool
Returns true if the string contains only hexadecimal chars, false otherwise.
$str = newStr('Acme');
echo$str->isHexadecimal();
// falseParameters:nothing
Return:
- bool
Return true if this is valid ipv4 address
$str = newStr('1.0.1.0');
echo$str->isIpV4();
// trueParameters:nothing
Return:
- bool
Return true if this is valid ipv6 address
$str = newStr('2001:cdba::3257:9652');
echo$str->isIpV6();
// trueParameters:nothing
Return:
- bool
Returns true if the string is JSON, false otherwise. Unlike json_decode in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, in that an empty string is not considered valid JSON.
$str = newStr('Acme');
echo$str->isJson();
// falseParameters:nothing
Return:
- bool
Returns true if the string contains only lower case chars, false otherwise.
$str = newStr('Acme');
echo$str->isLowerCase();
// falseParameters:nothing
Return:
- bool
Returns true if the string is serialized, false otherwise.
$str = newStr('Acme');
echo$str->isSerialized();
// falseParameters:nothing
Return:
- bool
It doesn't matter whether the given UUID has dashes.
$str = newStr('76d7cac8-1bd7-11e8-accf-0ed5f89f718b');
echo$str->isUUIDv4();
// false$str = newStr('ae815123-537f-4eb3-a9b8-35881c29e1ac');
echo$str->isUUIDv4();
// trueParameters:nothing
Return:
- bool
Returns true if the string contains only upper case chars, false otherwise.
$str = newStr('Acme');
echo$str->isUpperCase();
// falseParameters:nothing
Return:
- bool
Joins the original string with an array of other strings with the given $separator.
$str = newStr('foo');
echo$str->join('*', ['bar', 'baz']);
// foo*bar*bazParameters:
- string $separator
- array $otherStrings
Return:
- \Str
Returns the first $length characters of the string.
$str = newStr('/Acme/');
echo (string)$str->last(2);
// e/Parameters:
- int $length
Return:
- \Str
Returns the length of the string.
$str = newStr('/Acme/');
echo$str->length();
// 6Parameters:nothing
Return:
- int
Splits on newlines and carriage returns, returning an array of strings corresponding to the lines in the string.
$str = newStr("Acme\r\nAcme");
echo$str->lines();
// ['Acme', 'Acme']Parameters:nothing
Return:
- array
Returns the longest common prefix between the string and $otherStr.
$str = newStr('Acme');
echo (string)$str->longestCommonPrefix('Accurate');
// AcParameters:
- string $otherStr
Return:
- \Str
Returns the longest common substring between the string and $otherStr. In the case of ties, it returns that which occurs first.
$str = newStr('Acme');
echo (string)$str->longestCommonSubstring('meh');
// meParameters:
- string $otherStr
Return:
- \Str
Returns the longest common suffix between the string and $otherStr.
$str = newStr('Acme');
echo (string)$str->longestCommonSuffix('Do believe me');
// meParameters:
- string $otherStr
Return:
- \Str
Converts the first character of the string to lower case.
$str = newStr('Acme Foo');
echo (string)$str->lowerCaseFirst();
// acme FooParameters:nothing
Return:
- \Str
Create a new Str object using static method for it.
$str = Str::make('Acme');
echo (string)$str; // AcmeParameters:
- string $str
Return:
- \Str
Returns true if the string match regexp pattern
$s = newStr('foo baR');
echo$str->matchesPattern('.*aR');
// trueParameters:
- string $pattern
Return:
- bool
Move substring of desired $length to $destination index of the original string. In case $destination is less than $length returns the string untouched.
$str = newStr('/Acme/');
echo (string)$str->move(0, 2, 4);
// cm/Ae/Parameters:
- int $start
- int $length
- int $destination
Return:
- \Str
Replaces substring in the original string of $length with given $substr.
$str = newStr('/Acme/');
echo (string)$str->overwrite(0, 2, 'BAR');
// BARcme/Parameters:
- int $start
- int $length
- string $substr
Return:
- \Str
Returns a new string of a given length such that both sides of the string are padded.
$str = newStr('Acme');
echo (string)$str->padBoth(6, '/');
// /Acme/Parameters:
- int $length
- string $padStr
Return:
- \Str
Returns a new string of a given length such that the beginning of the string is padded.
$str = newStr('Acme/');
echo (string)$str->padLeft(6, '/');
// /Acme/Parameters:
- int $length
- string $padStr
Return:
- \Str
Returns a new string of a given length such that the end of the string is padded.
$str = newStr('/Acme');
echo (string)$str->padRight(6, '/');
// /Acme/Parameters:
- int $length
- string $padStr
Return:
- \Str
Returns the substring of the string from the last occurrence of $delimiter to the end.
$str = newStr('Acme/foo');
echo$str->pop('/');
// fooParameters:
- string $delimiter
Return:
- \Str
Returns the substring of the original string from the beginning to the last occurrence of $delimiter.
$str = newStr('Acme/foo/bar');
echo$str->popReversed('/');
// Acme/fooParameters:
- string $delimiter
Return:
- \Str
Prepend $sub to the string.
$str = newStr('Acme/');
echo (string)$str->prepend('/');
// /Acme/Parameters:
- string $sub
Return:
- \Str
Wraps each word in the string with specified $quote.
$str = newStr('foo bar baz');
echo$str->quote('*');
// *foo* *bar* *baz*Parameters:
- string $quote
Return:
- \Str
Generates a random string consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax. If $possibleChars is not specified, the generated string will consist of ASCII alphanumeric chars.
$str = newStr('foo bar');
echo$str->random(3, -1, 'fobarz');
// zfa$str = newStr('');
echo$str->random(3);
// 1hoParameters:
- int $size
- int $sizeMax
- string $possibleChars
Return:
- \Str
Replaces all occurrences of $pattern in the string by $replacement. An alias for mb_ereg_replace(). Note that the 'i' option with multi-byte patterns in mb_ereg_replace() requires PHP 5.6+ for correct results. This is due to a lack of support in the bundled version of Oniguruma in PHP < 5.6, and current versions of HHVM (3.8 and below).
$str = newStr('Acme Foo');
echo (string)$str->regexReplace('A', 'a');
// acme FooParameters:
- string $pattern
- string $replacement
- string $options
Return:
- \Str
Returns the string with the prefix $substring removed, if present.
$str = newStr('/Acme/');
echo (string)$str->removeLeft('/');
// Acme/Parameters:
- string $substring
Return:
- \Str
Returns the string with the suffix $substring removed, if present.
$str = newStr('/Acme/');
echo (string)$str->removeRight('/');
// /AcmeParameters:
- string $substring
Return:
- \Str
Returns a repeated string given a $multiplier. An alias for str_repeat.
$str = newStr('Acme/');
echo (string)$str->repeat(2);
// Acme/Acme/Parameters:
- int $multiplier
Return:
- \Str
Replaces all occurrences of $old in the string by $new.
$str = newStr('/Acme/');
echo (string)$str->replace('/', '#');
// #Acme#Parameters:
- string $old
- string $new
Return:
- \Str
Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.
$str = newStr('/Acme/');
echo (string)$str->replaceWithLimit('/', '#', 1);
// #Acme/Parameters:
- string $old
- string $new
- int $limit
Return:
- \Str
Returns a reversed string. A multi-byte version of strrev().
$str = newStr('/Acme/');
echo (string)$str->reverse();
// /emcA/Parameters:nothing
Return:
- \Str
Truncates the string to a given $length, while ensuring that it does not split words. If $substring is provided, and truncating occurs, the string is further truncated so that the $substring may be appended without exceeding the desired length.
$str = newStr('What are your plans today?');
echo (string)$str->safeTruncate(22, '...');
// What are your plans...Parameters:
- int $length
- string $substring
Return:
- \Str
Returns the substring of the original string from beginning to the first occurrence of $delimiter.
$str = newStr('Acme/foo');
echo$str->shift('/');
// AcmeParameters:
- string $delimiter
Return:
- \Str
Returns the substring of the original string from the first occurrence of $delimiter to the end.
$str = newStr('Acme/foo/bar');
echo$str->shiftReversed('/');
// foo/barParameters:
- string $delimiter
Return:
- \Str
A multi-byte str_shuffle() function. It returns a string with its characters in random order.
$str = newStr('/Acme/');
echo (string)$str->shuffle();
// mAe//cParameters:nothing
Return:
- \Str
Returns the substring beginning at $start, and up to, but not including the index specified by $end. If $end is omitted, the function extracts the remaining string. If $end is negative, it is computed from the end of the string.
$str = newStr('Acme');
echo (string)$str->slice(2);
// meParameters:
- int $start
- int|null $end
Return:
- \Str
Converts the string into an URL slug. This includes replacing non-ASCII characters with their closest ASCII equivalents, removing remaining non-ASCII and non-alphanumeric characters, and replacing whitespace with $replacement. The $replacement defaults to a single dash, and the string is also converted to lowercase. The $language of the source string can also be supplied for language-specific transliteration.
$str = newStr('Acme foo bar!');
echo (string)$str->slugify();
// acme-foo-barParameters:
- string $replacement
- string $language
Return:
- \Str
Returns a snake_case version of the string.
$str = newStr('Foo Bar');
echo (string)$str->snakeize();
// foo_barParameters:nothing
Return:
- \Str
Splits the string with the provided $pattern, returning an array of strings. An optional integer $limit will truncate the results.
$str = newStr('Acme#Acme');
echo$str->split('#', 1);
// ['Acme']Parameters:
- string $pattern
- int $limit
Return:
- array
Returns true if the string begins with $substring, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = newStr('/Accmme/');
echo$str->startsWith('/A');
// trueParameters:
- string $substring
- bool $caseSensitive
Return:
- bool
Returns true if the string begins with any of $substrings, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$str = newStr('/Accmme/');
echo$str->startsWithAny(['foo', '/A', 'bar']);
// trueParameters:
- array $substrings
- bool $caseSensitive
Return:
- bool
Strip all whitespace characters. This includes tabs and newline characters, as well as multi-byte whitespace such as the thin space and ideographic space.
$str = newStr('Acme foo');
echo (string)$str->stripWhitespace();
// AcmefooParameters:nothing
Return:
- \Str
Returns the substring beginning at $start with the specified $length. It differs from the mb_substr() function in that providing a $length of 0 will return the rest of the string, rather than an empty string.
$str = newStr('/Acme/');
echo (string)$str->substr(1, 4);
// AcmeParameters:
- int $start
- int $length
Return:
- \Str
Surrounds the string with the given $substring.
$str = newStr('Acme');
echo (string)$str->surround('/');
// /Acme/Parameters:
- string $substring
Return:
- \Str
Returns a case swapped version of the string.
$str = newStr('foObARbAz');
echo (string)$str->swapCase();
// FOoBarBaZParameters:nothing
Return:
- \Str
Returns a string with smart quotes, ellipsis characters, and dashes from Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
$str = newStr('“I see…”');
echo (string)$str->tidy();
// "I see..."Parameters:nothing
Return:
- \Str
Returns a trimmed string with the first letter of each word capitalized. Also accepts an array, $ignore, allowing you to list words not to be capitalized.
$str = newStr('i like to watch DVDs at home');
echo (string)$str->titleize(['at', 'to', 'the']);
// I Like to Watch Dvds at HomeParameters:
- array $ignore
Return:
- \Str
Returns an ASCII version of the string. A set of non-ASCII characters are replaced with their closest ASCII counterparts, and the rest are removed by default. The $language or locale of the source string can be supplied for language-specific transliteration in any of the following formats: en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping to "aeoeue" rather than "aou" as in other languages.
$str = newStr('Äcmế');
echo (string)$str->toAscii();
// AcmeParameters:
- string $language
- bool $removeUnsupported
Return:
- \Str
Returns a boolean representation of the given logical string value. For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', 'off', and 'no' will return false. In all instances, case is ignored. For other numeric strings, their sign will determine the return value. In addition, blank strings consisting of only whitespace will return false. For all other strings, the return value is a result of a boolean cast.
$str = newStr('yes');
echo$str->toBoolean();
// trueParameters:nothing
Return:
- bool
Make the string lowercase.
$str = newStr('/Acme/');
echo (string)$str->toLowerCase();
// /acme/Parameters:nothing
Return:
- \Str
Converts each tab in the string to some number of spaces, as defined by $tabLength. By default, each tab is converted to 4 consecutive spaces.
$str = newStr('foo bar');
echo (string)$str->toSpaces(0);
// foobarParameters:
- int $tabLength
Return:
- \Str
Converts each occurrence of some consecutive number of spaces, as defined by $tabLength, to a tab. By default, each 4 consecutive spaces are converted to a tab.
$str = newStr('foo bar');
echo (string)$str->toTabs();
// foo barParameters:
- int $tabLength
Return:
- \Str
Converts the first character of each word in the string to uppercase.
$str = newStr('foo bar baz');
echo (string)$str->toTitleCase();
// Foo Bar BazParameters:nothing
Return:
- \Str
Make the string uppercase.
$str = newStr('/Acme/');
echo (string)$str->toUpperCase();
// /ACME/Parameters:nothing
Return:
- \Str
Returns a string with whitespace removed from the start and end of the string. Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.
$str = newStr('/Acme/');
echo (string)$str->trim('/');
// AcmeParameters:
- string $chars
Return:
- \Str
Returns a string with whitespace removed from the start of the string. Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.
$str = newStr('/Acme/');
echo (string)$str->trimLeft('/');
// Acme/Parameters:
- string $chars
Return:
- \Str
Returns a string with whitespace removed from the end of the string. Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.
$str = newStr('/Acme/');
echo (string)$str->trimRight('/');
// /AcmeParameters:
- string $chars
Return:
- \Str
Truncates the string to a given $length. If $substring is provided, and truncating occurs, the string is further truncated so that the substring may be appended without exceeding the desired length.
$str = newStr('What are your plans today?');
echo (string)$str->truncate(19, '...');
// What are your pl...Parameters:
- int $length
- string $substring
Return:
- \Str
Returns a lowercase and trimmed string separated by underscores. Underscores are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as dashes.
$str = newStr('foo Bar baz');
echo (string)$str->underscored();
// foo_bar_bazParameters:nothing
Return:
- \Str
Unwraps each word in the original string, deleting the specified $quote.
$str = newStr('*foo* bar* ***baz*');
echo$str->unquote('*');
// foo bar bazParameters:
- string $quote
Return:
- \Str
Returns an UpperCamelCase version of the string. It trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, underscores.
$str = newStr('foo bar baz');
echo (string)$str->upperCamelize();
// FooBarBazParameters:nothing
Return:
- \Str
Converts the first character of the string to upper case.
$str = newStr('acme foo');
echo (string)$str->upperCaseFirst();
// Acme fooParameters:nothing
Return:
- \Str
Splits on whitespace, returning an array of strings corresponding to the words in the string.
$str = newStr('foo bar baz');
echo$str->words();
// ['foo', 'bar', 'baz']Parameters:nothing
Return:
- array
lib code tests (versus):
make lib-code-testshow to get total RANK:
make rankgenerate md:
make mdrun tests:
make testTest subjects:
- FS (str/str)
- Stringy (danielstjules/Stringy)
RANK (sum time of all benchmarks): smaller - is better!
| Target | Total Time | Diff |
|---|---|---|
| Str | 5.505 s. | 1x |
| Stringy | 10.840 s. | 2.0x |
| subject | mode | mem_peak | diff |
|---|---|---|---|
| bench_common_Str | 811.098μs | 1,929,728b | 1.00x |
| bench_common_Stringy | 5,310.290μs | 1,879,272b | 6.55x |
Please use php cs fixer before commit: https://github.com/FriendsOfPHP/PHP-CS-Fixer
you can add watcher in any IDE for automatic fix code style on save.