About
str/str
, (*1)
$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()); // same
Install
Requirements:
- php7.1, (*2)
composer require str/str
Features
- [x] strongly typed
- [x] no exceptions thrown
- [x] fast
- [x] new functions
A fast string manipulation library with multi-byte support.
Inspired by the "Stringy" library, with focus on speed., (*3)
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., (*4)
Functions Index:
A
- afterFirst
- afterLast
- append
- appendUniqueIdentifier
- at, (*5)
B
- beforeFirst
- beforeLast
- between, (*6)
C
- camelize
- chars
- chop
- collapseWhitespace
- contains
- containsAll
- containsAny
- countSubstr, (*7)
D
- dasherize
- delimit, (*8)
E
- endsWith
- endsWithAny
- ensureLeft
- ensureRight, (*9)
F
- first, (*10)
G
- getString, (*11)
H
- hasLowerCase
- hasPrefix
- hasSuffix
- hasUpperCase
- htmlDecode
- htmlEncode
- humanize, (*12)
I
- indexOf
- indexOfLast
- insert
- isAlpha
- isAlphanumeric
- isBase64
- isBlank
- isEmail
- isHexadecimal
- isIpV4
- isIpV6
- isJson
- isLowerCase
- isSerialized
- isUUIDv4
- isUpperCase, (*13)
J
- join, (*14)
L
- last
- length
- lines
- longestCommonPrefix
- longestCommonSubstring
- longestCommonSuffix
- lowerCaseFirst, (*15)
M
- make
- matchesPattern
- move, (*16)
O
- overwrite, (*17)
P
- padBoth
- padLeft
- padRight
- pop
- popReversed
- prepend, (*18)
Q
- quote, (*19)
R
- random
- regexReplace
- removeLeft
- removeRight
- repeat
- replace
- replaceWithLimit
- reverse, (*20)
S
- safeTruncate
- shift
- shiftReversed
- shuffle
- slice
- slugify
- snakeize
- split
- startsWith
- startsWithAny
- stripWhitespace
- substr
- surround
- swapCase, (*21)
T
- tidy
- titleize
- toAscii
- toBoolean
- toLowerCase
- toSpaces
- toTabs
- toTitleCase
- toUpperCase
- trim
- trimLeft
- trimRight
- truncate, (*22)
U
- underscored
- unquote
- upperCamelize
- upperCaseFirst, (*23)
W
- words, (*24)
Functions List:
afterFirst
Inserts given $substr $times into the original string after
the first occurrence of $needle., (*25)
$str = new Str('foo bar baz');
echo (string)$str->afterFirst('a', 'duh', 2);
// foo baduhduhr baz
Parameters:
- string $needle
- string $substr
- int $times, (*26)
Return:, (*27)
- \Str
afterLast
Inserts given $substr $times into the original string after
the last occurrence of $needle., (*28)
$str = new Str('foo bar baz');
echo (string)$str->afterLast('a', 'duh', 2);
// foo bar baduhduhz
Parameters:
- string $needle
- string $substr
- int $times, (*29)
Return:, (*30)
- \Str
append
Append $sub to the string., (*31)
$str = new Str('/Acme');
echo (string)$str->append('/');
// /Acme/
Parameters:
- string $sub, (*32)
Return:, (*33)
- \Str
appendUniqueIdentifier
Appends a random string consisting of $possibleChars, if specified, of given $size or
random length between $size and $sizeMax to the original string., (*34)
$str = new Str('foo');
echo $str->appendUniqueIdentifier(3, -1, 'foba_rz');
// foozro
Parameters:
- int $size
- int $sizeMax
- string $possibleChars, (*35)
Return:, (*36)
- \Str
at
Returns the character at $pos, with indexes starting at 0., (*37)
$str = new Str('/Acme/');
echo (string)$str->at(2);
// c
Parameters:
- int $pos, (*38)
Return:, (*39)
- \Str
beforeFirst
Inserts given $substr $times into the original string before
the first occurrence of $needle., (*40)
$str = new Str('foo bar baz');
echo (string)$str->beforeFirst('a', 'duh');
// foo bduhar baz
Parameters:
- string $needle
- string $substr
- int $times, (*41)
Return:, (*42)
- \Str
beforeLast
Inserts given $substr $times into the original string before
the last occurrence of $needle., (*43)
$str = new Str('foo bar baz');
echo (string)$str->beforeLast('a', 'duh');
// foo bar bduhaz
Parameters:
- string $needle
- string $substr
- int $times, (*44)
Return:, (*45)
- \Str
between
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., (*46)
$str = new Str('/Acme/');
echo (string)$str->between('/', '/');
// Acme
Parameters:
- string $start
- string $end
- int $offset, (*47)
Return:, (*48)
- \Str
camelize
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., (*49)
$str = new Str('ac me');
echo (string)$str->camelize();
// acMe
Parameters:
nothing, (*50)
Return:, (*51)
- \Str
chars
Returns an array consisting of the characters in the string., (*52)
$str = new Str('/Acme/');
echo (string)$str->chars();
// ['/', 'A', 'c', 'm', 'e', '/']
Parameters:
nothing, (*53)
Return:, (*54)
- array
chop
Cuts the original string in pieces of $step size., (*55)
$str = new Str('foo bar baz');
echo $str->chop(2);
// ['fo', 'o ', 'ba', 'r ', 'ba', 'z']
Parameters:
- int $step, (*56)
Return:, (*57)
- array
collapseWhitespace
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., (*58)
$str = new Str('foo bar baz');
echo (string)$str->collapseWhitespace();
// foo bar baz
Parameters:
nothing, (*59)
Return:, (*60)
- \Str
contains
Check if the string contains $needle substring., (*61)
$str = new Str('/Acme/');
echo $str->contains('/');
// true
$str = new Str('/Acme/');
echo $str->contains('a', false);
// true
Parameters:
- string $needle
- bool $caseSensitive, (*62)
Return:, (*63)
- bool
containsAll
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., (*64)
$str = new Str('/Accmme/');
echo $str->containsAll(['m', 'c', '/']);
// true
Parameters:
- array $needles
- bool $caseSensitive, (*65)
Return:, (*66)
- bool
containsAny
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., (*67)
$str = new Str('/Accmme/');
echo $str->containsAny(['foo', 'c', 'bar']);
// true
Parameters:
- array $needles
- bool $caseSensitive, (*68)
Return:, (*69)
- bool
countSubstr
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., (*70)
$str = new Str('/Accmme/');
echo $str->countSubstr('m');
// 2
Parameters:
- string $needle
- bool $caseSensitive, (*71)
Return:, (*72)
- int
dasherize
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., (*73)
$str = new Str('Ac me');
echo (string)$str->dasherize();
// ac-me
Parameters:
nothing, (*74)
Return:, (*75)
- \Str
delimit
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., (*76)
$str = new Str('Ac me');
echo (string)$str->delimit('#');
// ac#me
Parameters:
- $delimiter, (*77)
Return:, (*78)
- \Str
endsWith
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., (*79)
$str = new Str('/Accmme/');
echo $str->endsWith('e/');
// true
Parameters:
- string $substring
- bool $caseSensitive, (*80)
Return:, (*81)
- bool
endsWithAny
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., (*82)
$str = new Str('/Accmme/');
echo $str->endsWithAny(['foo', 'e/', 'bar']);
// true
Parameters:
- array $substrings
- bool $caseSensitive, (*83)
Return:, (*84)
- bool
ensureLeft
Check whether $prefix exists in the string, and prepend $prefix to the string if it doesn't., (*85)
$str = new Str('Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/
$str = new Str('/Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/
Parameters:
- string $check, (*86)
Return:, (*87)
- \Str
ensureRight
Check whether $suffix exists in the string, and append $suffix to the string if it doesn't., (*88)
$str = new Str('/Acme');
echo (string)$str->ensureRight('/'); // /Acme/
$str = new Str('/Acme/');
echo (string)$str->ensureRight('/'); // /Acme/
Parameters:
- string $check, (*89)
Return:, (*90)
- \Str
first
Returns the first $length characters of the string., (*91)
$str = new Str('/Acme/');
echo (string)$str->first(2);
// /A
Parameters:
- int $length, (*92)
Return:, (*93)
- \Str
getString
Parameters:
nothing, (*94)
Return:, (*95)
- string
hasLowerCase
Returns true if the string contains a lower case char, false otherwise., (*96)
$str = new Str('Acme');
echo $str->hasLowerCase();
// true
Parameters:
nothing, (*97)
Return:, (*98)
- bool
hasPrefix
Check if the string has $prefix at the start., (*99)
$str = new Str('/Acme/');
echo $str->hasPrefix('/');
// true
Parameters:
- string $prefix, (*100)
Return:, (*101)
- bool
hasSuffix
Check if the string has $suffix at the end., (*102)
$str = new Str('/Acme/');
echo $str->hasSuffix('/');
// true
Parameters:
- string $suffix, (*103)
Return:, (*104)
- bool
hasUpperCase
Returns true if the string contains an upper case char, false otherwise., (*105)
$str = new Str('Acme');
echo $str->hasUpperCase();
// true
Parameters:
nothing, (*106)
Return:, (*107)
- bool
htmlDecode
Convert all HTML entities to their applicable characters. An alias of html_entity_decode.
For a list of flags, refer
to PHP documentation., (*108)
$str = new Str('<Acme>');
echo (string)$str->htmlDecode();
// <Acme>
Parameters:
- int $flags, (*109)
Return:, (*110)
- \Str
htmlEncode
Convert all applicable characters to HTML entities. An alias of htmlentities.
Refer to PHP documentation
for a list of flags., (*111)
$str = new Str('<Acme>');
echo (string)$str->htmlEncode();
// <Acme>
Parameters:
- int $flags, (*112)
Return:, (*113)
- \Str
humanize
Capitalizes the first word of the string, replaces underscores with spaces., (*114)
$str = new Str('foo_id');
echo (string)$str->humanize();
// Foo
Parameters:
nothing, (*115)
Return:, (*116)
- \Str
indexOf
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., (*117)
$str = new Str('/Accmme/');
echo $str->indexOf('m');
// 4
Parameters:
- string $needle
- int $offset, (*118)
Return:, (*119)
- int
indexOfLast
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., (*120)
$str = new Str('/Accmme/');
echo $str->indexOfLast('m');
// 5
Parameters:
- string $needle
- int $offset, (*121)
Return:, (*122)
- int
insert
Inserts $substring into the string at the $index provided., (*123)
$str = new Str('/Ace/');
echo (string)$str->insert('m', 3);
// /Acme/
Parameters:
- string $substring
- int $index, (*124)
Return:, (*125)
- \Str
isAlpha
Returns true if the string contains only alphabetic chars, false otherwise., (*126)
$str = new Str('Acme');
echo $str->isAlpha();
// true
Parameters:
nothing, (*127)
Return:, (*128)
- bool
isAlphanumeric
Returns true if the string contains only alphabetic and numeric chars, false otherwise., (*129)
$str = new Str('Acme1');
echo $str->isAlphanumeric();
// true
Parameters:
nothing, (*130)
Return:, (*131)
- bool
isBase64
Check if this string is valid base64 encoded
data. Function do encode(decode(s)) === s,
so this is not so fast., (*132)
Parameters:
nothing, (*133)
Return:, (*134)
- bool
isBlank
Returns true if the string contains only whitespace chars, false otherwise., (*135)
$str = new Str('Acme');
echo $str->isBlank();
// false
Parameters:
nothing, (*136)
Return:, (*137)
- bool
isEmail
Splits the original string in pieces by '@' delimiter and returns
true in case the resulting array consists of 2 parts., (*138)
$str = new Str('test@test@example.com');
echo $str->isEmail();
// false
Parameters:
nothing, (*139)
Return:, (*140)
- bool
isHexadecimal
Returns true if the string contains only hexadecimal chars, false otherwise., (*141)
$str = new Str('Acme');
echo $str->isHexadecimal();
// false
Parameters:
nothing, (*142)
Return:, (*143)
- bool
isIpV4
Return true if this is valid ipv4 address, (*144)
$str = new Str('1.0.1.0');
echo $str->isIpV4();
// true
Parameters:
nothing, (*145)
Return:, (*146)
- bool
isIpV6
Return true if this is valid ipv6 address, (*147)
$str = new Str('2001:cdba::3257:9652');
echo $str->isIpV6();
// true
Parameters:
nothing, (*148)
Return:, (*149)
- bool
isJson
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., (*150)
$str = new Str('Acme');
echo $str->isJson();
// false
Parameters:
nothing, (*151)
Return:, (*152)
- bool
isLowerCase
Returns true if the string contains only lower case chars, false otherwise., (*153)
$str = new Str('Acme');
echo $str->isLowerCase();
// false
Parameters:
nothing, (*154)
Return:, (*155)
- bool
isSerialized
Returns true if the string is serialized, false otherwise., (*156)
$str = new Str('Acme');
echo $str->isSerialized();
// false
Parameters:
nothing, (*157)
Return:, (*158)
- bool
isUUIDv4
It doesn't matter whether the given UUID has dashes., (*159)
$str = new Str('76d7cac8-1bd7-11e8-accf-0ed5f89f718b');
echo $str->isUUIDv4();
// false
$str = new Str('ae815123-537f-4eb3-a9b8-35881c29e1ac');
echo $str->isUUIDv4();
// true
Parameters:
nothing, (*160)
Return:, (*161)
- bool
isUpperCase
Returns true if the string contains only upper case chars, false otherwise., (*162)
$str = new Str('Acme');
echo $str->isUpperCase();
// false
Parameters:
nothing, (*163)
Return:, (*164)
- bool
join
Joins the original string with an array of other strings with the given $separator., (*165)
$str = new Str('foo');
echo $str->join('*', ['bar', 'baz']);
// foo*bar*baz
Parameters:
- string $separator
- array $otherStrings, (*166)
Return:, (*167)
- \Str
last
Returns the first $length characters of the string., (*168)
$str = new Str('/Acme/');
echo (string)$str->last(2);
// e/
Parameters:
- int $length, (*169)
Return:, (*170)
- \Str
length
Returns the length of the string., (*171)
$str = new Str('/Acme/');
echo $str->length();
// 6
Parameters:
nothing, (*172)
Return:, (*173)
- int
lines
Splits on newlines and carriage returns, returning an array of strings
corresponding to the lines in the string., (*174)
$str = new Str("Acme\r\nAcme");
echo $str->lines();
// ['Acme', 'Acme']
Parameters:
nothing, (*175)
Return:, (*176)
- array
longestCommonPrefix
Returns the longest common prefix between the string and $otherStr., (*177)
$str = new Str('Acme');
echo (string)$str->longestCommonPrefix('Accurate');
// Ac
Parameters:
- string $otherStr, (*178)
Return:, (*179)
- \Str
longestCommonSubstring
Returns the longest common substring between the string and $otherStr.
In the case of ties, it returns that which occurs first., (*180)
$str = new Str('Acme');
echo (string)$str->longestCommonSubstring('meh');
// me
Parameters:
- string $otherStr, (*181)
Return:, (*182)
- \Str
longestCommonSuffix
Returns the longest common suffix between the string and $otherStr., (*183)
$str = new Str('Acme');
echo (string)$str->longestCommonSuffix('Do believe me');
// me
Parameters:
- string $otherStr, (*184)
Return:, (*185)
- \Str
lowerCaseFirst
Converts the first character of the string to lower case., (*186)
$str = new Str('Acme Foo');
echo (string)$str->lowerCaseFirst();
// acme Foo
Parameters:
nothing, (*187)
Return:, (*188)
- \Str
make
Create a new Str object using static method for it., (*189)
$str = Str::make('Acme');
echo (string)$str; // Acme
Parameters:
- string $str, (*190)
Return:, (*191)
- \Str
matchesPattern
Returns true if the string match regexp pattern, (*192)
$s = new Str('foo baR');
echo $str->matchesPattern('.*aR');
// true
Parameters:
- string $pattern, (*193)
Return:, (*194)
- bool
move
Move substring of desired $length to $destination index of the original string.
In case $destination is less than $length returns the string untouched., (*195)
$str = new Str('/Acme/');
echo (string)$str->move(0, 2, 4);
// cm/Ae/
Parameters:
- int $start
- int $length
- int $destination, (*196)
Return:, (*197)
- \Str
overwrite
Replaces substring in the original string of $length with given $substr., (*198)
$str = new Str('/Acme/');
echo (string)$str->overwrite(0, 2, 'BAR');
// BARcme/
Parameters:
- int $start
- int $length
- string $substr, (*199)
Return:, (*200)
- \Str
padBoth
Returns a new string of a given length such that both sides of the string are padded., (*201)
$str = new Str('Acme');
echo (string)$str->padBoth(6, '/');
// /Acme/
Parameters:
- int $length
- string $padStr, (*202)
Return:, (*203)
- \Str
padLeft
Returns a new string of a given length such that the beginning of the string is padded., (*204)
$str = new Str('Acme/');
echo (string)$str->padLeft(6, '/');
// /Acme/
Parameters:
- int $length
- string $padStr, (*205)
Return:, (*206)
- \Str
padRight
Returns a new string of a given length such that the end of the string is padded., (*207)
$str = new Str('/Acme');
echo (string)$str->padRight(6, '/');
// /Acme/
Parameters:
- int $length
- string $padStr, (*208)
Return:, (*209)
- \Str
pop
Returns the substring of the string from the last occurrence of $delimiter to the end., (*210)
$str = new Str('Acme/foo');
echo $str->pop('/');
// foo
Parameters:
- string $delimiter, (*211)
Return:, (*212)
- \Str
popReversed
Returns the substring of the original string from the beginning
to the last occurrence of $delimiter., (*213)
$str = new Str('Acme/foo/bar');
echo $str->popReversed('/');
// Acme/foo
Parameters:
- string $delimiter, (*214)
Return:, (*215)
- \Str
prepend
Prepend $sub to the string., (*216)
$str = new Str('Acme/');
echo (string)$str->prepend('/');
// /Acme/
Parameters:
- string $sub, (*217)
Return:, (*218)
- \Str
quote
Wraps each word in the string with specified $quote., (*219)
$str = new Str('foo bar baz');
echo $str->quote('*');
// *foo* *bar* *baz*
Parameters:
- string $quote, (*220)
Return:, (*221)
- \Str
random
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., (*222)
$str = new Str('foo bar');
echo $str->random(3, -1, 'fobarz');
// zfa
$str = new Str('');
echo $str->random(3);
// 1ho
Parameters:
- int $size
- int $sizeMax
- string $possibleChars, (*223)
Return:, (*224)
- \Str
regexReplace
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)., (*225)
$str = new Str('Acme Foo');
echo (string)$str->regexReplace('A', 'a');
// acme Foo
Parameters:
- string $pattern
- string $replacement
- string $options, (*226)
Return:, (*227)
- \Str
removeLeft
Returns the string with the prefix $substring removed, if present., (*228)
$str = new Str('/Acme/');
echo (string)$str->removeLeft('/');
// Acme/
Parameters:
- string $substring, (*229)
Return:, (*230)
- \Str
removeRight
Returns the string with the suffix $substring removed, if present., (*231)
$str = new Str('/Acme/');
echo (string)$str->removeRight('/');
// /Acme
Parameters:
- string $substring, (*232)
Return:, (*233)
- \Str
repeat
Returns a repeated string given a $multiplier. An alias for str_repeat., (*234)
$str = new Str('Acme/');
echo (string)$str->repeat(2);
// Acme/Acme/
Parameters:
- int $multiplier, (*235)
Return:, (*236)
- \Str
replace
Replaces all occurrences of $old in the string by $new., (*237)
$str = new Str('/Acme/');
echo (string)$str->replace('/', '#');
// #Acme#
Parameters:
- string $old
- string $new, (*238)
Return:, (*239)
- \Str
replaceWithLimit
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., (*240)
$str = new Str('/Acme/');
echo (string)$str->replaceWithLimit('/', '#', 1);
// #Acme/
Parameters:
- string $old
- string $new
- int $limit, (*241)
Return:, (*242)
- \Str
reverse
Returns a reversed string. A multi-byte version of strrev()., (*243)
$str = new Str('/Acme/');
echo (string)$str->reverse();
// /emcA/
Parameters:
nothing, (*244)
Return:, (*245)
- \Str
safeTruncate
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., (*246)
$str = new Str('What are your plans today?');
echo (string)$str->safeTruncate(22, '...');
// What are your plans...
Parameters:
- int $length
- string $substring, (*247)
Return:, (*248)
- \Str
shift
Returns the substring of the original string from beginning to the first occurrence
of $delimiter., (*249)
$str = new Str('Acme/foo');
echo $str->shift('/');
// Acme
Parameters:
- string $delimiter, (*250)
Return:, (*251)
- \Str
shiftReversed
Returns the substring of the original string from the first occurrence of $delimiter to the end., (*252)
$str = new Str('Acme/foo/bar');
echo $str->shiftReversed('/');
// foo/bar
Parameters:
- string $delimiter, (*253)
Return:, (*254)
- \Str
shuffle
A multi-byte str_shuffle() function. It returns a string with its characters in random order., (*255)
$str = new Str('/Acme/');
echo (string)$str->shuffle();
// mAe//c
Parameters:
nothing, (*256)
Return:, (*257)
- \Str
slice
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., (*258)
$str = new Str('Acme');
echo (string)$str->slice(2);
// me
Parameters:
- int $start
- int|null $end, (*259)
Return:, (*260)
- \Str
slugify
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., (*261)
$str = new Str('Acme foo bar!');
echo (string)$str->slugify();
// acme-foo-bar
Parameters:
- string $replacement
- string $language, (*262)
Return:, (*263)
- \Str
snakeize
Returns a snake_case version of the string., (*264)
$str = new Str('Foo Bar');
echo (string)$str->snakeize();
// foo_bar
Parameters:
nothing, (*265)
Return:, (*266)
- \Str
split
Splits the string with the provided $pattern, returning an array of strings.
An optional integer $limit will truncate the results., (*267)
$str = new Str('Acme#Acme');
echo $str->split('#', 1);
// ['Acme']
Parameters:
- string $pattern
- int $limit, (*268)
Return:, (*269)
- array
startsWith
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., (*270)
$str = new Str('/Accmme/');
echo $str->startsWith('/A');
// true
Parameters:
- string $substring
- bool $caseSensitive, (*271)
Return:, (*272)
- bool
startsWithAny
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., (*273)
$str = new Str('/Accmme/');
echo $str->startsWithAny(['foo', '/A', 'bar']);
// true
Parameters:
- array $substrings
- bool $caseSensitive, (*274)
Return:, (*275)
- bool
stripWhitespace
Strip all whitespace characters. This includes tabs and newline characters,
as well as multi-byte whitespace such as the thin space and ideographic space., (*276)
$str = new Str('Acme foo');
echo (string)$str->stripWhitespace();
// Acmefoo
Parameters:
nothing, (*277)
Return:, (*278)
- \Str
substr
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., (*279)
$str = new Str('/Acme/');
echo (string)$str->substr(1, 4);
// Acme
Parameters:
- int $start
- int $length, (*280)
Return:, (*281)
- \Str
surround
Surrounds the string with the given $substring., (*282)
$str = new Str('Acme');
echo (string)$str->surround('/');
// /Acme/
Parameters:
- string $substring, (*283)
Return:, (*284)
- \Str
swapCase
Returns a case swapped version of the string., (*285)
$str = new Str('foObARbAz');
echo (string)$str->swapCase();
// FOoBarBaZ
Parameters:
nothing, (*286)
Return:, (*287)
- \Str
tidy
Returns a string with smart quotes, ellipsis characters, and dashes from Windows-1252
(commonly used in Word documents) replaced by their ASCII equivalents., (*288)
$str = new Str('“I see…”');
echo (string)$str->tidy();
// "I see..."
Parameters:
nothing, (*289)
Return:, (*290)
- \Str
titleize
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., (*291)
$str = new Str('i like to watch DVDs at home');
echo (string)$str->titleize(['at', 'to', 'the']);
// I Like to Watch Dvds at Home
Parameters:
- array $ignore, (*292)
Return:, (*293)
- \Str
toAscii
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., (*294)
$str = new Str('Äcmế');
echo (string)$str->toAscii();
// Acme
Parameters:
- string $language
- bool $removeUnsupported, (*295)
Return:, (*296)
- \Str
toBoolean
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., (*297)
$str = new Str('yes');
echo $str->toBoolean();
// true
Parameters:
nothing, (*298)
Return:, (*299)
- bool
toLowerCase
Make the string lowercase., (*300)
$str = new Str('/Acme/');
echo (string)$str->toLowerCase();
// /acme/
Parameters:
nothing, (*301)
Return:, (*302)
- \Str
toSpaces
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., (*303)
$str = new Str('foo bar');
echo (string)$str->toSpaces(0);
// foobar
Parameters:
- int $tabLength, (*304)
Return:, (*305)
- \Str
toTabs
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., (*306)
$str = new Str('foo bar');
echo (string)$str->toTabs();
// foo bar
Parameters:
- int $tabLength, (*307)
Return:, (*308)
- \Str
toTitleCase
Converts the first character of each word in the string to uppercase., (*309)
$str = new Str('foo bar baz');
echo (string)$str->toTitleCase();
// Foo Bar Baz
Parameters:
nothing, (*310)
Return:, (*311)
- \Str
toUpperCase
Make the string uppercase., (*312)
$str = new Str('/Acme/');
echo (string)$str->toUpperCase();
// /ACME/
Parameters:
nothing, (*313)
Return:, (*314)
- \Str
trim
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., (*315)
$str = new Str('/Acme/');
echo (string)$str->trim('/');
// Acme
Parameters:
- string $chars, (*316)
Return:, (*317)
- \Str
trimLeft
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., (*318)
$str = new Str('/Acme/');
echo (string)$str->trimLeft('/');
// Acme/
Parameters:
- string $chars, (*319)
Return:, (*320)
- \Str
trimRight
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., (*321)
$str = new Str('/Acme/');
echo (string)$str->trimRight('/');
// /Acme
Parameters:
- string $chars, (*322)
Return:, (*323)
- \Str
truncate
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., (*324)
$str = new Str('What are your plans today?');
echo (string)$str->truncate(19, '...');
// What are your pl...
Parameters:
- int $length
- string $substring, (*325)
Return:, (*326)
- \Str
underscored
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., (*327)
$str = new Str('foo Bar baz');
echo (string)$str->underscored();
// foo_bar_baz
Parameters:
nothing, (*328)
Return:, (*329)
- \Str
unquote
Unwraps each word in the original string, deleting the specified $quote., (*330)
$str = new Str('*foo* bar* ***baz*');
echo $str->unquote('*');
// foo bar baz
Parameters:
- string $quote, (*331)
Return:, (*332)
- \Str
upperCamelize
Returns an UpperCamelCase version of the string. It trims surrounding spaces,
capitalizes letters following digits, spaces, dashes and underscores,
and removes spaces, dashes, underscores., (*333)
$str = new Str('foo bar baz');
echo (string)$str->upperCamelize();
// FooBarBaz
Parameters:
nothing, (*334)
Return:, (*335)
- \Str
upperCaseFirst
Converts the first character of the string to upper case., (*336)
$str = new Str('acme foo');
echo (string)$str->upperCaseFirst();
// Acme foo
Parameters:
nothing, (*337)
Return:, (*338)
- \Str
words
Splits on whitespace, returning an array of strings corresponding to the words in the string., (*339)
$str = new Str('foo bar baz');
echo $str->words();
// ['foo', 'bar', 'baz']
Parameters:
nothing, (*340)
Return:, (*341)
- array
Benchmark
lib code tests (versus):, (*342)
make lib-code-tests
how to get total RANK:, (*343)
make rank
generate md:, (*344)
make md
run tests:, (*345)
make test
Test subjects:
- FS (str/str)
- Stringy (danielstjules/Stringy), (*346)
RANK (sum time of all benchmarks):
smaller - is better!, (*347)
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 |
Development
Please use php cs fixer before commit:
https://github.com/FriendsOfPHP/PHP-CS-Fixer, (*348)
you can add watcher in any IDE for automatic fix code
style on save., (*349)