PHP Class Stringy\Stringy

Inheritance: implements Countable, implements IteratorAggregate, implements ArrayAccess
ファイルを表示 Open project: danielstjules/stringy Class Usage Examples

Protected Properties

Property Type Description
$encoding string The string's encoding, which should be one of the mbstring module's supported encodings.
$str string An instance's string.

Public Methods

Method Description
__construct ( mixed $str = '', string $encoding = null ) Initializes a Stringy object and assigns both str and encoding properties the supplied values. $str is cast to a string prior to assignment, and if $encoding is not specified, it defaults to mb_internal_encoding(). Throws an InvalidArgumentException if the first argument is an array or object without a __toString method.
__toString ( ) : string Returns the value in $str.
append ( string $string ) : Stringy Returns a new string with $string appended.
at ( integer $index ) : Stringy Returns the character at $index, with indexes starting at 0.
between ( string $start, string $end, integer $offset ) : Stringy 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.
camelize ( ) : Stringy 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.
chars ( ) : array Returns an array consisting of the characters in the string.
collapseWhitespace ( ) : Stringy Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multibyte whitespace such as the thin space and ideographic space.
contains ( string $needle, boolean $caseSensitive = true ) : boolean Returns true if the string contains $needle, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
containsAll ( array $needles, boolean $caseSensitive = true ) : boolean 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.
containsAny ( array $needles, boolean $caseSensitive = true ) : boolean 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.
count ( ) : integer Returns the length of the string, implementing the countable interface.
countSubstr ( string $substring, boolean $caseSensitive = true ) : integer Returns the number of occurrences of $substring in the given string.
create ( mixed $str = '', string $encoding = null ) : Stringy Creates a Stringy object and assigns both str and encoding properties the supplied values. $str is cast to a string prior to assignment, and if $encoding is not specified, it defaults to mb_internal_encoding(). It then returns the initialized object. Throws an InvalidArgumentException if the first argument is an array or object without a __toString method.
dasherize ( ) : Stringy 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.
delimit ( string $delimiter ) : Stringy Returns a lowercase and trimmed string separated by the given delimiter.
endsWith ( string $substring, boolean $caseSensitive = true ) : boolean 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.
ensureLeft ( string $substring ) : Stringy Ensures that the string begins with $substring. If it doesn't, it's prepended.
ensureRight ( string $substring ) : Stringy Ensures that the string ends with $substring. If it doesn't, it's appended.
first ( integer $n ) : Stringy Returns the first $n characters of the string.
getEncoding ( ) : string Returns the encoding used by the Stringy object.
getIterator ( ) : ArrayIterator Returns a new ArrayIterator, thus implementing the IteratorAggregate interface. The ArrayIterator's constructor is passed an array of chars in the multibyte string. This enables the use of foreach with instances of Stringy\Stringy.
hasLowerCase ( ) : boolean Returns true if the string contains a lower case char, false otherwise.
hasUpperCase ( ) : boolean Returns true if the string contains an upper case char, false otherwise.
htmlDecode ( integer | null $flags = ENT_COMPAT ) : Stringy Convert all HTML entities to their applicable characters. An alias of html_entity_decode. For a list of flags, refer to http://php.net/manual/en/function.html-entity-decode.php
htmlEncode ( integer | null $flags = ENT_COMPAT ) : Stringy Convert all applicable characters to HTML entities. An alias of htmlentities. Refer to http://php.net/manual/en/function.htmlentities.php for a list of flags.
humanize ( ) : Stringy Capitalizes the first word of the string, replaces underscores with spaces, and strips '_id'.
indexOf ( string $needle, integer $offset ) : integer | boolean Returns the index of the first occurrence of $needle in the string, and false if not found. Accepts an optional offset from which to begin the search.
indexOfLast ( string $needle, integer $offset ) : integer | boolean 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.
insert ( string $substring, integer $index ) : Stringy Inserts $substring into the string at the $index provided.
isAlpha ( ) : boolean Returns true if the string contains only alphabetic chars, false otherwise.
isAlphanumeric ( ) : boolean Returns true if the string contains only alphabetic and numeric chars, false otherwise.
isBase64 ( ) : boolean Returns true if the string is base64 encoded, false otherwise.
isBlank ( ) : boolean Returns true if the string contains only whitespace chars, false otherwise.
isHexadecimal ( ) : boolean Returns true if the string contains only hexadecimal chars, false otherwise.
isJson ( ) : boolean 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.
isLowerCase ( ) : boolean Returns true if the string contains only lower case chars, false otherwise.
isSerialized ( ) : boolean Returns true if the string is serialized, false otherwise.
isUpperCase ( ) : boolean Returns true if the string contains only lower case chars, false otherwise.
last ( integer $n ) : Stringy Returns the last $n characters of the string.
length ( ) : integer Returns the length of the string. An alias for PHP's mb_strlen() function.
lines ( ) : Stringy[] Splits on newlines and carriage returns, returning an array of Stringy objects corresponding to the lines in the string.
longestCommonPrefix ( string $otherStr ) : Stringy Returns the longest common prefix between the string and $otherStr.
longestCommonSubstring ( string $otherStr ) : Stringy Returns the longest common substring between the string and $otherStr.
longestCommonSuffix ( string $otherStr ) : Stringy Returns the longest common suffix between the string and $otherStr.
lowerCaseFirst ( ) : Stringy Converts the first character of the string to lower case.
offsetExists ( mixed $offset ) : boolean Returns whether or not a character exists at an index. Offsets may be negative to count from the last character in the string. Implements part of the ArrayAccess interface.
offsetGet ( mixed $offset ) : mixed Returns the character at the given index. Offsets may be negative to count from the last character in the string. Implements part of the ArrayAccess interface, and throws an OutOfBoundsException if the index does not exist.
offsetSet ( mixed $offset, mixed $value ) Implements part of the ArrayAccess interface, but throws an exception when called. This maintains the immutability of Stringy objects.
offsetUnset ( mixed $offset ) Implements part of the ArrayAccess interface, but throws an exception when called. This maintains the immutability of Stringy objects.
pad ( integer $length, string $padStr = ' ', string $padType = 'right' ) : Stringy Pads the string to a given length with $padStr. If length is less than or equal to the length of the string, no padding takes places. The default string used for padding is a space, and the default type (one of 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException if $padType isn't one of those 3 values.
padBoth ( integer $length, string $padStr = ' ' ) : Stringy Returns a new string of a given length such that both sides of the string are padded. Alias for pad() with a $padType of 'both'.
padLeft ( integer $length, string $padStr = ' ' ) : Stringy Returns a new string of a given length such that the beginning of the string is padded. Alias for pad() with a $padType of 'left'.
padRight ( integer $length, string $padStr = ' ' ) : Stringy Returns a new string of a given length such that the end of the string is padded. Alias for pad() with a $padType of 'right'.
prepend ( string $string ) : Stringy Returns a new string starting with $string.
regexReplace ( string $pattern, string $replacement, string $options = 'msr' ) : Stringy Replaces all occurrences of $pattern in $str by $replacement. An alias for mb_ereg_replace(). Note that the 'i' option with multibyte 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).
removeLeft ( string $substring ) : Stringy Returns a new string with the prefix $substring removed, if present.
removeRight ( string $substring ) : Stringy Returns a new string with the suffix $substring removed, if present.
repeat ( integer $multiplier ) : Stringy Returns a repeated string given a multiplier. An alias for str_repeat.
replace ( string $search, string $replacement ) : Stringy Replaces all occurrences of $search in $str by $replacement.
reverse ( ) : Stringy Returns a reversed string. A multibyte version of strrev().
safeTruncate ( integer $length, string $substring = '' ) : Stringy 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.
shuffle ( ) * A multibyte str_shuffle() function. It returns a string with its characters in random order.
slice ( integer $start, integer $end = null ) : Stringy 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.
slugify ( string $replacement = '-' ) : Stringy 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.
split ( string $pattern, integer $limit = null ) : Stringy[] Splits the string with the provided regular expression, returning an array of Stringy objects. An optional integer $limit will truncate the results.
startsWith ( string $substring, boolean $caseSensitive = true ) : boolean 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.
substr ( integer $start, integer $length = null ) : Stringy Returns the substring beginning at $start with the specified $length.
surround ( string $substring ) : Stringy Surrounds $str with the given substring.
swapCase ( ) : Stringy Returns a case swapped version of the string.
tidy ( ) : Stringy Returns a string with smart quotes, ellipsis characters, and dashes from Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
titleize ( array $ignore = null ) : Stringy Returns a trimmed string with the first letter of each word capitalized.
toAscii ( boolean $removeUnsupported = true ) : Stringy 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 unless instructed otherwise.
toBoolean ( ) : boolean Returns a boolean representation of the given logical string value.
toLowerCase ( ) : Stringy Converts all characters in the string to lowercase. An alias for PHP's mb_strtolower().
toSpaces ( integer $tabLength = 4 ) : Stringy 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.
toTabs ( integer $tabLength = 4 ) : Stringy 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.
toTitleCase ( ) : Stringy Converts the first character of each word in the string to uppercase.
toUpperCase ( ) : Stringy Converts all characters in the string to uppercase. An alias for PHP's mb_strtoupper().
trim ( string $chars = null ) : Stringy 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.
trimLeft ( string $chars = null ) : Stringy Returns a string with whitespace removed from the start of the string.
trimRight ( string $chars = null ) : Stringy Returns a string with whitespace removed from the end of the string.
truncate ( integer $length, string $substring = '' ) : Stringy 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.
underscored ( ) : Stringy Returns a lowercase and trimmed string separated by underscores.
upperCamelize ( ) : Stringy Returns an UpperCamelCase version of the supplied string. It trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, underscores.
upperCaseFirst ( ) : Stringy Converts the first character of the supplied string to upper case.

Protected Methods

Method Description
charsArray ( ) : array Returns the replacements for the toAscii() method.

Private Methods

Method Description
applyPadding ( integer $left, integer $right, string $padStr = ' ' ) : Stringy Adds the specified amount of left and right padding to the given string.
eregReplace ( $pattern, $replacement, $string, $option = 'msr' ) Alias for mb_ereg_replace with a fallback to preg_replace if the mbstring module is not installed.
matchesPattern ( string $pattern ) : boolean Returns true if $str matches the supplied pattern, false otherwise.
regexEncoding ( ) Alias for mb_regex_encoding which default to a noop if the mbstring module is not installed.
supportsEncoding ( )

Method Details

__construct() public method

Initializes a Stringy object and assigns both str and encoding properties the supplied values. $str is cast to a string prior to assignment, and if $encoding is not specified, it defaults to mb_internal_encoding(). Throws an InvalidArgumentException if the first argument is an array or object without a __toString method.
public __construct ( mixed $str = '', string $encoding = null )
$str mixed Value to modify, after being cast to string
$encoding string The character encoding

__toString() public method

Returns the value in $str.
public __toString ( ) : string
return string The current value of the $str property

append() public method

Returns a new string with $string appended.
public append ( string $string ) : Stringy
$string string The string to append
return Stringy Object with appended $string

at() public method

Returns the character at $index, with indexes starting at 0.
public at ( integer $index ) : Stringy
$index integer Position of the character
return Stringy The character at $index

between() public method

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.
public between ( string $start, string $end, integer $offset ) : Stringy
$start string Delimiter marking the start of the substring
$end string Delimiter marking the end of the substring
$offset integer Index from which to begin the search
return Stringy Object whose $str is a substring between $start and $end

camelize() public method

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.
public camelize ( ) : Stringy
return Stringy Object with $str in camelCase

chars() public method

Returns an array consisting of the characters in the string.
public chars ( ) : array
return array An array of string chars

charsArray() protected method

Returns the replacements for the toAscii() method.
protected charsArray ( ) : array
return array An array of replacements.

collapseWhitespace() public method

Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multibyte whitespace such as the thin space and ideographic space.
public collapseWhitespace ( ) : Stringy
return Stringy Object with a trimmed $str and condensed whitespace

contains() public method

Returns true if the string contains $needle, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
public contains ( string $needle, boolean $caseSensitive = true ) : boolean
$needle string Substring to look for
$caseSensitive boolean Whether or not to enforce case-sensitivity
return boolean Whether or not $str contains $needle

containsAll() public method

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.
public containsAll ( array $needles, boolean $caseSensitive = true ) : boolean
$needles array Substrings to look for
$caseSensitive boolean Whether or not to enforce case-sensitivity
return boolean Whether or not $str contains $needle

containsAny() public method

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.
public containsAny ( array $needles, boolean $caseSensitive = true ) : boolean
$needles array Substrings to look for
$caseSensitive boolean Whether or not to enforce case-sensitivity
return boolean Whether or not $str contains $needle

count() public method

Returns the length of the string, implementing the countable interface.
public count ( ) : integer
return integer The number of characters in the string, given the encoding

countSubstr() public method

By default, the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
public countSubstr ( string $substring, boolean $caseSensitive = true ) : integer
$substring string The substring to search for
$caseSensitive boolean Whether or not to enforce case-sensitivity
return integer The number of $substring occurrences

create() public static method

Creates a Stringy object and assigns both str and encoding properties the supplied values. $str is cast to a string prior to assignment, and if $encoding is not specified, it defaults to mb_internal_encoding(). It then returns the initialized object. Throws an InvalidArgumentException if the first argument is an array or object without a __toString method.
public static create ( mixed $str = '', string $encoding = null ) : Stringy
$str mixed Value to modify, after being cast to string
$encoding string The character encoding
return Stringy A Stringy object

dasherize() public method

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.
public dasherize ( ) : Stringy
return Stringy Object with a dasherized $str

delimit() public method

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.
public delimit ( string $delimiter ) : Stringy
$delimiter string Sequence used to separate parts of the string
return Stringy Object with a delimited $str

endsWith() public method

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.
public endsWith ( string $substring, boolean $caseSensitive = true ) : boolean
$substring string The substring to look for
$caseSensitive boolean Whether or not to enforce case-sensitivity
return boolean Whether or not $str ends with $substring

ensureLeft() public method

Ensures that the string begins with $substring. If it doesn't, it's prepended.
public ensureLeft ( string $substring ) : Stringy
$substring string The substring to add if not present
return Stringy Object with its $str prefixed by the $substring

ensureRight() public method

Ensures that the string ends with $substring. If it doesn't, it's appended.
public ensureRight ( string $substring ) : Stringy
$substring string The substring to add if not present
return Stringy Object with its $str suffixed by the $substring

first() public method

Returns the first $n characters of the string.
public first ( integer $n ) : Stringy
$n integer Number of characters to retrieve from the start
return Stringy Object with its $str being the first $n chars

getEncoding() public method

Returns the encoding used by the Stringy object.
public getEncoding ( ) : string
return string The current value of the $encoding property

getIterator() public method

Returns a new ArrayIterator, thus implementing the IteratorAggregate interface. The ArrayIterator's constructor is passed an array of chars in the multibyte string. This enables the use of foreach with instances of Stringy\Stringy.
public getIterator ( ) : ArrayIterator
return ArrayIterator An iterator for the characters in the string

hasLowerCase() public method

Returns true if the string contains a lower case char, false otherwise.
public hasLowerCase ( ) : boolean
return boolean Whether or not the string contains a lower case character.

hasUpperCase() public method

Returns true if the string contains an upper case char, false otherwise.
public hasUpperCase ( ) : boolean
return boolean Whether or not the string contains an upper case character.

htmlDecode() public method

Convert all HTML entities to their applicable characters. An alias of html_entity_decode. For a list of flags, refer to http://php.net/manual/en/function.html-entity-decode.php
public htmlDecode ( integer | null $flags = ENT_COMPAT ) : Stringy
$flags integer | null Optional flags
return Stringy Object with the resulting $str after being html decoded.

htmlEncode() public method

Convert all applicable characters to HTML entities. An alias of htmlentities. Refer to http://php.net/manual/en/function.htmlentities.php for a list of flags.
public htmlEncode ( integer | null $flags = ENT_COMPAT ) : Stringy
$flags integer | null Optional flags
return Stringy Object with the resulting $str after being html encoded.

humanize() public method

Capitalizes the first word of the string, replaces underscores with spaces, and strips '_id'.
public humanize ( ) : Stringy
return Stringy Object with a humanized $str

indexOf() public method

Returns the index of the first occurrence of $needle in the string, and false if not found. Accepts an optional offset from which to begin the search.
public indexOf ( string $needle, integer $offset ) : integer | boolean
$needle string Substring to look for
$offset integer Offset from which to search
return integer | boolean The occurrence's index if found, otherwise false

indexOfLast() public method

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.
public indexOfLast ( string $needle, integer $offset ) : integer | boolean
$needle string Substring to look for
$offset integer Offset from which to search
return integer | boolean The last occurrence's index if found, otherwise false

insert() public method

Inserts $substring into the string at the $index provided.
public insert ( string $substring, integer $index ) : Stringy
$substring string String to be inserted
$index integer The index at which to insert the substring
return Stringy Object with the resulting $str after the insertion

isAlpha() public method

Returns true if the string contains only alphabetic chars, false otherwise.
public isAlpha ( ) : boolean
return boolean Whether or not $str contains only alphabetic chars

isAlphanumeric() public method

Returns true if the string contains only alphabetic and numeric chars, false otherwise.
public isAlphanumeric ( ) : boolean
return boolean Whether or not $str contains only alphanumeric chars

isBase64() public method

Returns true if the string is base64 encoded, false otherwise.
public isBase64 ( ) : boolean
return boolean Whether or not $str is base64 encoded

isBlank() public method

Returns true if the string contains only whitespace chars, false otherwise.
public isBlank ( ) : boolean
return boolean Whether or not $str contains only whitespace characters

isHexadecimal() public method

Returns true if the string contains only hexadecimal chars, false otherwise.
public isHexadecimal ( ) : boolean
return boolean Whether or not $str contains only hexadecimal chars

isJson() public method

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.
public isJson ( ) : boolean
return boolean Whether or not $str is JSON

isLowerCase() public method

Returns true if the string contains only lower case chars, false otherwise.
public isLowerCase ( ) : boolean
return boolean Whether or not $str contains only lower case characters

isSerialized() public method

Returns true if the string is serialized, false otherwise.
public isSerialized ( ) : boolean
return boolean Whether or not $str is serialized

isUpperCase() public method

Returns true if the string contains only lower case chars, false otherwise.
public isUpperCase ( ) : boolean
return boolean Whether or not $str contains only lower case characters

last() public method

Returns the last $n characters of the string.
public last ( integer $n ) : Stringy
$n integer Number of characters to retrieve from the end
return Stringy Object with its $str being the last $n chars

length() public method

Returns the length of the string. An alias for PHP's mb_strlen() function.
public length ( ) : integer
return integer The number of characters in $str given the encoding

lines() public method

Splits on newlines and carriage returns, returning an array of Stringy objects corresponding to the lines in the string.
public lines ( ) : Stringy[]
return Stringy[] An array of Stringy objects

longestCommonPrefix() public method

Returns the longest common prefix between the string and $otherStr.
public longestCommonPrefix ( string $otherStr ) : Stringy
$otherStr string Second string for comparison
return Stringy Object with its $str being the longest common prefix

longestCommonSubstring() public method

In the case of ties, it returns that which occurs first.
public longestCommonSubstring ( string $otherStr ) : Stringy
$otherStr string Second string for comparison
return Stringy Object with its $str being the longest common substring

longestCommonSuffix() public method

Returns the longest common suffix between the string and $otherStr.
public longestCommonSuffix ( string $otherStr ) : Stringy
$otherStr string Second string for comparison
return Stringy Object with its $str being the longest common suffix

lowerCaseFirst() public method

Converts the first character of the string to lower case.
public lowerCaseFirst ( ) : Stringy
return Stringy Object with the first character of $str being lower case

offsetExists() public method

Returns whether or not a character exists at an index. Offsets may be negative to count from the last character in the string. Implements part of the ArrayAccess interface.
public offsetExists ( mixed $offset ) : boolean
$offset mixed The index to check
return boolean Whether or not the index exists

offsetGet() public method

Returns the character at the given index. Offsets may be negative to count from the last character in the string. Implements part of the ArrayAccess interface, and throws an OutOfBoundsException if the index does not exist.
public offsetGet ( mixed $offset ) : mixed
$offset mixed The index from which to retrieve the char
return mixed The character at the specified index

offsetSet() public method

Implements part of the ArrayAccess interface, but throws an exception when called. This maintains the immutability of Stringy objects.
public offsetSet ( mixed $offset, mixed $value )
$offset mixed The index of the character
$value mixed Value to set

offsetUnset() public method

Implements part of the ArrayAccess interface, but throws an exception when called. This maintains the immutability of Stringy objects.
public offsetUnset ( mixed $offset )
$offset mixed The index of the character

pad() public method

Pads the string to a given length with $padStr. If length is less than or equal to the length of the string, no padding takes places. The default string used for padding is a space, and the default type (one of 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException if $padType isn't one of those 3 values.
public pad ( integer $length, string $padStr = ' ', string $padType = 'right' ) : Stringy
$length integer Desired string length after padding
$padStr string String used to pad, defaults to space
$padType string One of 'left', 'right', 'both'
return Stringy Object with a padded $str

padBoth() public method

Returns a new string of a given length such that both sides of the string are padded. Alias for pad() with a $padType of 'both'.
public padBoth ( integer $length, string $padStr = ' ' ) : Stringy
$length integer Desired string length after padding
$padStr string String used to pad, defaults to space
return Stringy String with padding applied

padLeft() public method

Returns a new string of a given length such that the beginning of the string is padded. Alias for pad() with a $padType of 'left'.
public padLeft ( integer $length, string $padStr = ' ' ) : Stringy
$length integer Desired string length after padding
$padStr string String used to pad, defaults to space
return Stringy String with left padding

padRight() public method

Returns a new string of a given length such that the end of the string is padded. Alias for pad() with a $padType of 'right'.
public padRight ( integer $length, string $padStr = ' ' ) : Stringy
$length integer Desired string length after padding
$padStr string String used to pad, defaults to space
return Stringy String with right padding

prepend() public method

Returns a new string starting with $string.
public prepend ( string $string ) : Stringy
$string string The string to append
return Stringy Object with appended $string

regexReplace() public method

Replaces all occurrences of $pattern in $str by $replacement. An alias for mb_ereg_replace(). Note that the 'i' option with multibyte 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).
public regexReplace ( string $pattern, string $replacement, string $options = 'msr' ) : Stringy
$pattern string The regular expression pattern
$replacement string The string to replace with
$options string Matching conditions to be used
return Stringy Object with the resulting $str after the replacements

removeLeft() public method

Returns a new string with the prefix $substring removed, if present.
public removeLeft ( string $substring ) : Stringy
$substring string The prefix to remove
return Stringy Object having a $str without the prefix $substring

removeRight() public method

Returns a new string with the suffix $substring removed, if present.
public removeRight ( string $substring ) : Stringy
$substring string The suffix to remove
return Stringy Object having a $str without the suffix $substring

repeat() public method

Returns a repeated string given a multiplier. An alias for str_repeat.
public repeat ( integer $multiplier ) : Stringy
$multiplier integer The number of times to repeat the string
return Stringy Object with a repeated str

replace() public method

Replaces all occurrences of $search in $str by $replacement.
public replace ( string $search, string $replacement ) : Stringy
$search string The needle to search for
$replacement string The string to replace with
return Stringy Object with the resulting $str after the replacements

reverse() public method

Returns a reversed string. A multibyte version of strrev().
public reverse ( ) : Stringy
return Stringy Object with a reversed $str

safeTruncate() public method

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.
public safeTruncate ( integer $length, string $substring = '' ) : Stringy
$length integer Desired length of the truncated string
$substring string The substring to append if it can fit
return Stringy Object with the resulting $str after truncating

shuffle() public method

* A multibyte str_shuffle() function. It returns a string with its characters in random order.
public shuffle ( )

slice() public method

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.
public slice ( integer $start, integer $end = null ) : Stringy
$start integer Initial index from which to begin extraction
$end integer Optional index at which to end extraction
return Stringy Object with its $str being the extracted substring

slugify() public method

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.
public slugify ( string $replacement = '-' ) : Stringy
$replacement string The string used to replace whitespace
return Stringy Object whose $str has been converted to an URL slug

split() public method

Splits the string with the provided regular expression, returning an array of Stringy objects. An optional integer $limit will truncate the results.
public split ( string $pattern, integer $limit = null ) : Stringy[]
$pattern string The regex with which to split the string
$limit integer Optional maximum number of results to return
return Stringy[] An array of Stringy objects

startsWith() public method

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.
public startsWith ( string $substring, boolean $caseSensitive = true ) : boolean
$substring string The substring to look for
$caseSensitive boolean Whether or not to enforce case-sensitivity
return boolean Whether or not $str starts with $substring

substr() public method

It differs from the mb_substr() function in that providing a $length of null will return the rest of the string, rather than an empty string.
public substr ( integer $start, integer $length = null ) : Stringy
$start integer Position of the first character to use
$length integer Maximum number of characters used
return Stringy Object with its $str being the substring

surround() public method

Surrounds $str with the given substring.
public surround ( string $substring ) : Stringy
$substring string The substring to add to both sides
return Stringy Object whose $str had the substring both prepended and appended

swapCase() public method

Returns a case swapped version of the string.
public swapCase ( ) : Stringy
return Stringy Object whose $str has each character's case swapped

tidy() public method

Returns a string with smart quotes, ellipsis characters, and dashes from Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
public tidy ( ) : Stringy
return Stringy Object whose $str has those characters removed

titleize() public method

Also accepts an array, $ignore, allowing you to list words not to be capitalized.
public titleize ( array $ignore = null ) : Stringy
$ignore array An array of words not to capitalize
return Stringy Object with a titleized $str

toAscii() public method

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 unless instructed otherwise.
public toAscii ( boolean $removeUnsupported = true ) : Stringy
$removeUnsupported boolean Whether or not to remove the unsupported characters
return Stringy Object whose $str contains only ASCII characters

toBoolean() public method

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.
public toBoolean ( ) : boolean
return boolean A boolean value for the string

toLowerCase() public method

Converts all characters in the string to lowercase. An alias for PHP's mb_strtolower().
public toLowerCase ( ) : Stringy
return Stringy Object with all characters of $str being lowercase

toSpaces() public method

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.
public toSpaces ( integer $tabLength = 4 ) : Stringy
$tabLength integer Number of spaces to replace each tab with
return Stringy Object whose $str has had tabs switched to spaces

toTabs() public method

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.
public toTabs ( integer $tabLength = 4 ) : Stringy
$tabLength integer Number of spaces to replace with a tab
return Stringy Object whose $str has had spaces switched to tabs

toTitleCase() public method

Converts the first character of each word in the string to uppercase.
public toTitleCase ( ) : Stringy
return Stringy Object with all characters of $str being title-cased

toUpperCase() public method

Converts all characters in the string to uppercase. An alias for PHP's mb_strtoupper().
public toUpperCase ( ) : Stringy
return Stringy Object with all characters of $str being uppercase

trim() public method

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.
public trim ( string $chars = null ) : Stringy
$chars string Optional string of characters to strip
return Stringy Object with a trimmed $str

trimLeft() public method

Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.
public trimLeft ( string $chars = null ) : Stringy
$chars string Optional string of characters to strip
return Stringy Object with a trimmed $str

trimRight() public method

Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.
public trimRight ( string $chars = null ) : Stringy
$chars string Optional string of characters to strip
return Stringy Object with a trimmed $str

truncate() public method

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.
public truncate ( integer $length, string $substring = '' ) : Stringy
$length integer Desired length of the truncated string
$substring string The substring to append if it can fit
return Stringy Object with the resulting $str after truncating

underscored() public method

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.
public underscored ( ) : Stringy
return Stringy Object with an underscored $str

upperCamelize() public method

Returns an UpperCamelCase version of the supplied string. It trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, underscores.
public upperCamelize ( ) : Stringy
return Stringy Object with $str in UpperCamelCase

upperCaseFirst() public method

Converts the first character of the supplied string to upper case.
public upperCaseFirst ( ) : Stringy
return Stringy Object with the first character of $str being upper case

Property Details

$encoding protected_oe property

The string's encoding, which should be one of the mbstring module's supported encodings.
protected string $encoding
return string

$str protected_oe property

An instance's string.
protected string $str
return string