$ 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 ( str ing $ s ) {};
$ send (( str ing ) $ str ); // same
$ send ( $ str -> get str ing ()); // same
str Requirements strong>:
composer require str / str
A fast str ing manipulation library with multi-byte support. Inspired by the " str ingy" library, with focus on speed.
Lib uses php7 features and does not throw any exceptions (because all input parameters are str ongly 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 $sub str $times into the original str ing after the first occurrence of $needle.
$ str = new str ( ' foo bar baz ' );
echo ( str ing ) $ str -> afterFirst ( ' a ' , ' duh ' , 2 );
// foo baduhduhr baz
str Parameters: strong>
str Return: strong>
Inserts given $sub str $times into the original str ing after the last occurrence of $needle.
$ str = new str ( ' foo bar baz ' );
echo ( str ing ) $ str -> afterLast ( ' a ' , ' duh ' , 2 );
// foo bar baduhduhz
str Parameters: strong>
str Return: strong>
Append $sub to the str ing.
$ str = new str ( ' /Acme ' );
echo ( str ing ) $ str -> append ( ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Appends a random str ing consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax to the original str ing.
$ str = new str ( ' foo ' );
echo $ str -> appendUniqueIdentifier ( 3 , - 1 , ' foba_rz ' );
// foozro
str Parameters: strong>
str Return: strong>
Returns the character at $pos, with indexes starting at 0.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> at ( 2 );
// c
str Parameters: strong>
str Return: strong>
Inserts given $sub str $times into the original str ing before the first occurrence of $needle.
$ str = new str ( ' foo bar baz ' );
echo ( str ing ) $ str -> beforeFirst ( ' a ' , ' duh ' );
// foo bduhar baz
str Parameters: strong>
str Return: strong>
Inserts given $sub str $times into the original str ing before the last occurrence of $needle.
$ str = new str ( ' foo bar baz ' );
echo ( str ing ) $ str -> beforeLast ( ' a ' , ' duh ' );
// foo bar bduhaz
str Parameters: strong>
str Return: strong>
Returns the sub str ing between $start and $end, if found, or an empty str ing. An optional $offset may be supplied from which to begin the search for the start str ing.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> between ( ' / ' , ' / ' );
// Acme
str Parameters: strong>
str Return: strong>
Returns a camelCase version of the str ing. 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 ( str ing ) $ str -> camelize ();
// acMe
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns an array consisting of the characters in the str ing.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> chars ();
// ['/', 'A', 'c', 'm', 'e', '/']
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Cuts the original str ing in pieces of $step size.
$ str = new str ( ' foo bar baz ' );
echo $ str -> chop ( 2 );
// ['fo', 'o ', 'ba', 'r ', 'ba', 'z']
str Parameters: strong>
str Return: strong>
Trims the str ing 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 ( str ing ) $ str -> collapseWhitespace ();
// foo bar baz
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Check if the str ing contains $needle sub str ing.
$ str = new str ( ' /Acme/ ' );
echo $ str -> contains ( ' / ' );
// true
$ str = new str ( ' /Acme/ ' );
echo $ str -> contains ( ' a ' , false );
// true
str Parameters: strong>
str Return: strong>
Returns true if the str ing 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 ' , ' / ' ]);
// true
str Parameters: strong>
str Return: strong>
Returns true if the str ing 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 ' ]);
// true
str Parameters: strong>
str Return: strong>
Returns the number of occurrences of $needle in the given str ing. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
$ str = new str ( ' /Accmme/ ' );
echo $ str -> countSub str ( ' m ' );
// 2
str Parameters: strong>
str Return: strong>
Returns a lowercase and trimmed str ing separated by dashes. Dashes are inserted before uppercase characters (with the exception of the first character of the str ing), and in place of spaces as well as underscores.
$ str = new str ( ' Ac me ' );
echo ( str ing ) $ str -> dasherize ();
// ac-me
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns a lowercase and trimmed str ing separated by the given $delimiter. Delimiters are inserted before uppercase characters (with the exception of the first character of the str ing), and in place of spaces, dashes, and underscores. Alpha delimiters are not converted to lower case.
$ str = new str ( ' Ac me ' );
echo ( str ing ) $ str -> delimit ( ' # ' );
// ac#me
str Parameters: strong>
str Return: strong>
Returns true if the str ing ends with $sub str ing, 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/ ' );
// true
str Parameters: strong>
str Return: strong>
Returns true if the str ing ends with any of $sub str ings, 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 ' ]);
// true
str Parameters: strong>
str Return: strong>
Check whether $prefix exists in the str ing, and prepend $prefix to the str ing if it doesn't.
$ str = new str ( ' Acme/ ' );
echo ( str ing ) $ str -> ensureLeft ( ' / ' );
// /Acme/
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> ensureLeft ( ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Check whether $suffix exists in the str ing, and append $suffix to the str ing if it doesn't.
$ str = new str ( ' /Acme ' );
echo ( str ing ) $ str -> ensureRight ( ' / ' ); // /Acme/
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> ensureRight ( ' / ' ); // /Acme/
str Parameters: strong>
str Return: strong>
Returns the first $length characters of the str ing.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> first ( 2 );
// /A
str Parameters: strong>
str Return: strong>
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing contains a lower case char, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> hasLowerCase ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Check if the str ing has $prefix at the start.
$ str = new str ( ' /Acme/ ' );
echo $ str -> hasPrefix ( ' / ' );
// true
str Parameters: strong>
str Return: strong>
Check if the str ing has $suffix at the end.
$ str = new str ( ' /Acme/ ' );
echo $ str -> hasSuffix ( ' / ' );
// true
str Parameters: strong>
str Return: strong>
Returns true if the str ing contains an upper case char, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> hasUpperCase ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
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 ( str ing ) $ str -> htmlDecode ();
// <Acme>
str Parameters: strong>
str Return: strong>
Convert all applicable characters to HTML entities. An alias of htmlentities. Refer to PHP documentation for a list of flags.
$ str = new str ( ' <Acme> ' );
echo ( str ing ) $ str -> htmlEncode ();
// <Acme>
str Parameters: strong>
str Return: strong>
Capitalizes the first word of the str ing, replaces underscores with spaces.
$ str = new str ( ' foo_id ' );
echo ( str ing ) $ str -> humanize ();
// Foo
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns the index of the first occurrence of $needle in the str ing, and -1 if not found. Accepts an optional $offset from which to begin the search.
$ str = new str ( ' /Accmme/ ' );
echo $ str -> indexOf ( ' m ' );
// 4
str Parameters: strong>
str Return: strong>
Returns the index of the last occurrence of $needle in the str ing, 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 str ing.
$ str = new str ( ' /Accmme/ ' );
echo $ str -> indexOfLast ( ' m ' );
// 5
str Parameters: strong>
str Return: strong>
Inserts $sub str ing into the str ing at the $index provided.
$ str = new str ( ' /Ace/ ' );
echo ( str ing ) $ str -> insert ( ' m ' , 3 );
// /Acme/
str Parameters: strong>
str Return: strong>
Returns true if the str ing contains only alphabetic chars, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> isAlpha ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing contains only alphabetic and numeric chars, false otherwise.
$ str = new str ( ' Acme1 ' );
echo $ str -> isAlphanumeric ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Check if this str ing is valid base64 encoded data. Function do encode(decode(s)) === s, so this is not so fast.
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing contains only whitespace chars, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> isBlank ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Splits the original str ing in pieces by '@' delimiter and returns true in case the resulting array consists of 2 parts.
$ str = new str ( ' test@[email protected] ' );
echo $ str -> isEmail ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing contains only hexadecimal chars, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> isHexadecimal ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Return true if this is valid ipv4 address
$ str = new str ( ' 1.0.1.0 ' );
echo $ str -> isIpV4 ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Return true if this is valid ipv6 address
$ str = new str ( ' 2001:cdba::3257:9652 ' );
echo $ str -> isIpV6 ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing 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 str ing is not considered valid JSON.
$ str = new str ( ' Acme ' );
echo $ str -> isJson ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing contains only lower case chars, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> isLowerCase ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing is serialized, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> isSerialized ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
It doesn't matter whether the given UUID has dashes.
$ str = new str ( ' 76d7cac8-1bd7-11e8-accf-0ed5f89f718b ' );
echo $ str -> isUUIDv4 ();
// false
$ str = new str ( ' ae815123-537f-4eb3-a9b8-35881c29e1ac ' );
echo $ str -> isUUIDv4 ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns true if the str ing contains only upper case chars, false otherwise.
$ str = new str ( ' Acme ' );
echo $ str -> isUpperCase ();
// false
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Joins the original str ing with an array of other str ings with the given $separator.
$ str = new str ( ' foo ' );
echo $ str -> join ( ' * ' , [ ' bar ' , ' baz ' ]);
// foo*bar*baz
str Parameters: strong>
str Return: strong>
Returns the first $length characters of the str ing.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> last ( 2 );
// e/
str Parameters: strong>
str Return: strong>
Returns the length of the str ing.
$ str = new str ( ' /Acme/ ' );
echo $ str -> length ();
// 6
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Splits on newlines and carriage returns, returning an array of str ings corresponding to the lines in the str ing.
$ str = new str ( " Acme rn Acme " );
echo $ str -> lines ();
// ['Acme', 'Acme']
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns the longest common prefix between the str ing and $other str .
$ str = new str ( ' Acme ' );
echo ( str ing ) $ str -> longestCommonPrefix ( ' Accurate ' );
// Ac
str Parameters: strong>
str Return: strong>
Returns the longest common sub str ing between the str ing and $other str . In the case of ties, it returns that which occurs first.
$ str = new str ( ' Acme ' );
echo ( str ing ) $ str -> longestCommonSub str ing ( ' meh ' );
// me
str Parameters: strong>
str Return: strong>
Returns the longest common suffix between the str ing and $other str .
$ str = new str ( ' Acme ' );
echo ( str ing ) $ str -> longestCommonSuffix ( ' Do believe me ' );
// me
str Parameters: strong>
str Return: strong>
Converts the first character of the str ing to lower case.
$ str = new str ( ' Acme Foo ' );
echo ( str ing ) $ str -> lowerCaseFirst ();
// acme Foo
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Create a new str object using static method for it.
$ str = str :: make ( ' Acme ' );
echo ( str ing ) $ str ; // Acme
str Parameters: strong>
str Return: strong>
Returns true if the str ing match regexp pattern
$ s = new str ( ' foo baR ' );
echo $ str -> matchesPattern ( ' .*aR ' );
// true
str Parameters: strong>
str Return: strong>
Move sub str ing of desired $length to $destination index of the original str ing. In case $destination is less than $length returns the str ing untouched.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> move ( 0 , 2 , 4 );
// cm/Ae/
str Parameters: strong>
str Return: strong>
Replaces sub str ing in the original str ing of $length with given $sub str .
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> overwrite ( 0 , 2 , ' BAR ' );
// BARcme/
str Parameters: strong>
str Return: strong>
Returns a new str ing of a given length such that both sides of the str ing are padded.
$ str = new str ( ' Acme ' );
echo ( str ing ) $ str -> padBoth ( 6 , ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Returns a new str ing of a given length such that the beginning of the str ing is padded.
$ str = new str ( ' Acme/ ' );
echo ( str ing ) $ str -> padLeft ( 6 , ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Returns a new str ing of a given length such that the end of the str ing is padded.
$ str = new str ( ' /Acme ' );
echo ( str ing ) $ str -> padRight ( 6 , ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Returns the sub str ing of the str ing from the last occurrence of $delimiter to the end.
$ str = new str ( ' Acme/foo ' );
echo $ str -> pop ( ' / ' );
// foo
str Parameters: strong>
str Return: strong>
Returns the sub str ing of the original str ing from the beginning to the last occurrence of $delimiter.
$ str = new str ( ' Acme/foo/bar ' );
echo $ str -> popReversed ( ' / ' );
// Acme/foo
str Parameters: strong>
str Return: strong>
Prepend $sub to the str ing.
$ str = new str ( ' Acme/ ' );
echo ( str ing ) $ str -> prepend ( ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Wraps each word in the str ing with specified $quote.
$ str = new str ( ' foo bar baz ' );
echo $ str -> quote ( ' * ' );
// *foo* *bar* *baz*
str Parameters: strong>
str Return: strong>
Generates a random str ing consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax. If $possibleChars is not specified, the generated str ing will consist of ASCII alphanumeric chars.
$ str = new str ( ' foo bar ' );
echo $ str -> random ( 3 , - 1 , ' fobarz ' );
// zfa
$ str = new str ( '' );
echo $ str -> random ( 3 );
// 1ho
str Parameters: strong>
str Return: strong>
Replaces all occurrences of $pattern in the str ing 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 = new str ( ' Acme Foo ' );
echo ( str ing ) $ str -> regexReplace ( ' A ' , ' a ' );
// acme Foo
str Parameters: strong>
str Return: strong>
Returns the str ing with the prefix $sub str ing removed, if present.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> removeLeft ( ' / ' );
// Acme/
str Parameters: strong>
str Return: strong>
Returns the str ing with the suffix $sub str ing removed, if present.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> removeRight ( ' / ' );
// /Acme
str Parameters: strong>
str Return: strong>
Returns a repeated str ing given a $multiplier. An alias for str _repeat.
$ str = new str ( ' Acme/ ' );
echo ( str ing ) $ str -> repeat ( 2 );
// Acme/Acme/
str Parameters: strong>
str Return: strong>
Replaces all occurrences of $old in the str ing by $new.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> replace ( ' / ' , ' # ' );
// #Acme#
str Parameters: strong>
str Return: strong>
Replace returns a copy of the str ing s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the str ing and after each UTF-8 sequence, yielding up to k+ 1 replacements for a k-rune str ing. If n < 0, there is no limit on the number of replacements.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> replaceWithLimit ( ' / ' , ' # ' , 1 );
// #Acme/
str Parameters: strong>
str Return: strong>
Returns a reversed str ing. A multi-byte version of str rev().
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> reverse ();
// /emcA/
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Truncates the str ing to a given $length, while ensuring that it does not split words. If $sub str ing is provided, and truncating occurs, the str ing is further truncated so that the $sub str ing may be appended without exceeding the desired length.
$ str = new str ( ' What are your plans today? ' );
echo ( str ing ) $ str -> safeTruncate ( 22 , ' ... ' );
// What are your plans...
str Parameters: strong>
str Return: strong>
Returns the sub str ing of the original str ing from beginning to the first occurrence of $delimiter.
$ str = new str ( ' Acme/foo ' );
echo $ str -> shift ( ' / ' );
// Acme
str Parameters: strong>
str Return: strong>
Returns the sub str ing of the original str ing from the first occurrence of $delimiter to the end.
$ str = new str ( ' Acme/foo/bar ' );
echo $ str -> shiftReversed ( ' / ' );
// foo/bar
str Parameters: strong>
str Return: strong>
A multi-byte str _shuffle() function. It returns a str ing with its characters in random order.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> shuffle ();
// mAe//c
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns the sub str ing beginning at $start, and up to, but not including the index specified by $end. If $end is omitted, the function extracts the remaining str ing. If $end is negative, it is computed from the end of the str ing.
$ str = new str ( ' Acme ' );
echo ( str ing ) $ str -> slice ( 2 );
// me
str Parameters: strong>
str Return: strong>
Converts the str ing 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 str ing is also converted to lowercase. The $language of the source str ing can also be supplied for language-specific transliteration.
$ str = new str ( ' Acme foo bar! ' );
echo ( str ing ) $ str -> slugify ();
// acme-foo-bar
str Parameters: strong>
str Return: strong>
Returns a snake_case version of the str ing.
$ str = new str ( ' Foo Bar ' );
echo ( str ing ) $ str -> snakeize ();
// foo_bar
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Splits the str ing with the provided $pattern, returning an array of str ings. An optional integer $limit will truncate the results.
$ str = new str ( ' Acme#Acme ' );
echo $ str -> split ( ' # ' , 1 );
// ['Acme']
str Parameters: strong>
str Return: strong>
Returns true if the str ing begins with $sub str ing, 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 -> startsWith ( ' /A ' );
// true
str Parameters: strong>
str Return: strong>
Returns true if the str ing begins with any of $sub str ings, 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 -> startsWithAny ([ ' foo ' , ' /A ' , ' bar ' ]);
// true
str Parameters: strong>
str Return: strong>
str ip all whitespace characters. This includes tabs and newline characters, as well as multi-byte whitespace such as the thin space and ideographic space.
$ str = new str ( ' Acme foo ' );
echo ( str ing ) $ str -> str ipWhitespace ();
// Acmefoo
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns the sub str ing beginning at $start with the specified $length. It differs from the mb_sub str () function in that providing a $length of 0 will return the rest of the str ing, rather than an empty str ing.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> sub str ( 1 , 4 );
// Acme
str Parameters: strong>
str Return: strong>
Surrounds the str ing with the given $sub str ing.
$ str = new str ( ' Acme ' );
echo ( str ing ) $ str -> surround ( ' / ' );
// /Acme/
str Parameters: strong>
str Return: strong>
Returns a case swapped version of the str ing.
$ str = new str ( ' foObARbAz ' );
echo ( str ing ) $ str -> swapCase ();
// FOoBarBaZ
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns a str ing with smart quotes, ellipsis characters, and dashes from Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
$ str = new str ( ' “I see…” ' );
echo ( str ing ) $ str -> tidy ();
// "I see..."
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns a trimmed str ing with the first letter of each word capitalized. Also accepts an array, $ignore, allowing you to list words not to be capitalized.
$ str = new str ( ' i like to watch DVDs at home ' );
echo ( str ing ) $ str -> titleize ([ ' at ' , ' to ' , ' the ' ]);
// I Like to Watch Dvds at Home
str Parameters: strong>
str Return: strong>
Returns an ASCII version of the str ing. A set of non-ASCII characters are replaced with their closest ASCII allies, and the rest are removed by default. The $language or locale of the source str ing 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 = new str ( ' Äcmế ' );
echo ( str ing ) $ str -> toAscii ();
// Acme
str Parameters: strong>
str Return: strong>
Returns a boolean representation of the given logical str ing 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 str ings, their sign will determine the return value. In addition, blank str ings consisting of only whitespace will return false. For all other str ings, the return value is a result of a boolean cast.
$ str = new str ( ' yes ' );
echo $ str -> toBoolean ();
// true
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Make the str lowercase.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> toLowerCase ();
// /acme/
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Converts each tab in the str ing to some number of spaces, as defined by $tabLength. By default, each tab is converted to 4 consecutive spaces.
$ str = new str ( ' foo bar ' );
echo ( str ing ) $ str -> toSpaces ( 0 );
// foobar
str Parameters: strong>
str Return: strong>
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 = new str ( ' foo bar ' );
echo ( str ing ) $ str -> toTabs ();
// foo bar
str Parameters: strong>
str Return: strong>
Converts the first character of each word in the str ing to uppercase.
$ str = new str ( ' foo bar baz ' );
echo ( str ing ) $ str -> toTitleCase ();
// Foo Bar Baz
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Make the str uppercase.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> toUpperCase ();
// /ACME/
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Returns a str ing with whitespace removed from the start and end of the str ing. Supports the removal of unicode whitespace. Accepts an optional str ing of characters to str ip instead of the defaults.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> trim ( ' / ' );
// Acme
str Parameters: strong>
str Return: strong>
Returns a str ing with whitespace removed from the start of the str ing. Supports the removal of unicode whitespace. Accepts an optional str ing of characters to str ip instead of the defaults.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> trimLeft ( ' / ' );
// Acme/
str Parameters: strong>
str Return: strong>
Returns a str ing with whitespace removed from the end of the str ing. Supports the removal of unicode whitespace. Accepts an optional str ing of characters to str ip instead of the defaults.
$ str = new str ( ' /Acme/ ' );
echo ( str ing ) $ str -> trimRight ( ' / ' );
// /Acme
str Parameters: strong>
str Return: strong>
Truncates the str ing to a given $length. If $sub str ing is provided, and truncating occurs, the str ing is further truncated so that the sub str ing may be appended without exceeding the desired length.
$ str = new str ( ' What are your plans today? ' );
echo ( str ing ) $ str -> truncate ( 19 , ' ... ' );
// What are your pl...
str Parameters: strong>
str Return: strong>
Returns a lowercase and trimmed str ing separated by underscores. Underscores are inserted before uppercase characters (with the exception of the first character of the str ing), and in place of spaces as well as dashes.
$ str = new str ( ' foo Bar baz ' );
echo ( str ing ) $ str -> underscored ();
// foo_bar_baz
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Unwraps each word in the original str ing, deleting the specified $quote.
$ str = new str ( ' *foo* bar* ***baz* ' );
echo $ str -> unquote ( ' * ' );
// foo bar baz
str Parameters: strong>
str Return: strong>
Returns an UpperCamelCase version of the str ing. It trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, underscores.
$ str = new str ( ' foo bar baz ' );
echo ( str ing ) $ str -> upperCamelize ();
// FooBarBaz
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Converts the first character of the str ing to upper case.
$ str = new str ( ' acme foo ' );
echo ( str ing ) $ str -> upperCaseFirst ();
// Acme foo
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
Splits on whitespace, returning an array of str ings corresponding to the words in the str ing.
$ str = new str ( ' foo bar baz ' );
echo $ str -> words ();
// ['foo', 'bar', 'baz']
str Parameters: str ong> < str ong>nothing strong>
str Return: strong>
lib code tests (versus):
make lib-code-tests
how to get total RANK:
make rank
generate md:
make md
run tests:
make test
Test subjects:
RANK (sum time of all benchmarks): < str ong>smaller - is better! strong>
Target | Total Time | Diff |
---|---|---|
str | 5.505 s. | 1x |
str ingy | 10.840 s. | 2.0x |
subject | mode | mem_peak | diff |
---|---|---|---|
bench_common_ str | 811.098μs | 1,929,728b | 1.00x |
bench_common_str str | 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.