Final Class lispa\amos\core\utilities\StringUtils

Inheritancelispa\amos\core\utilities\StringUtils

Public Methods

Hide inherited methods

MethodDescriptionDefined By
capitalize() Capitalizes a string changing the first letter to upper case. lispa\amos\core\utilities\StringUtils
charAt() Returns the character at the specified index. lispa\amos\core\utilities\StringUtils
chomp() Removes one newline from end of a string if it's there, otherwise leave it alone. lispa\amos\core\utilities\StringUtils
chop() Remove the specified last character from a string. lispa\amos\core\utilities\StringUtils
compare() Compares two strings lexicographically. lispa\amos\core\utilities\StringUtils
compareIgnoreCase() Compares two strings lexicographically, ignoring case differences. lispa\amos\core\utilities\StringUtils
contains() lispa\amos\core\utilities\StringUtils
containsWord() lispa\amos\core\utilities\StringUtils
endsWith() Checks if a string ends with a specified suffix. lispa\amos\core\utilities\StringUtils
equal() Compares two strings, returning true if they are equal. lispa\amos\core\utilities\StringUtils
equalsIgnoreCase() Compares two strings, returning true if they are equal ignoring the case. lispa\amos\core\utilities\StringUtils
indexOf() Finds the first index within a string from a start position, handling null. lispa\amos\core\utilities\StringUtils
isBlank() Checks if a string is whitespace, empty ('') or null. lispa\amos\core\utilities\StringUtils
isEmpty() Checks if a string is empty ('') or null. lispa\amos\core\utilities\StringUtils
isNotBlank() Checks if a string is not empty (''), not null and not whitespace only. lispa\amos\core\utilities\StringUtils
isNotEmpty() Checks if a string is not empty ('') and not null. lispa\amos\core\utilities\StringUtils
lastIndexOf() Finds the first index within a string, handling null. lispa\amos\core\utilities\StringUtils
length() Returns the length of a string or 0 if the string is null. lispa\amos\core\utilities\StringUtils
lowerCase() Converts a string to lower case. lispa\amos\core\utilities\StringUtils
removeEnd() Removes a substring only if it is at the end of a source string, otherwise returns the source string. lispa\amos\core\utilities\StringUtils
removeStart() Removes a substring only if it is at the beginning of a source string, otherwise returns the source string. lispa\amos\core\utilities\StringUtils
repeat() Repeats a string the specified number of times to form a new string, with a specified string injected each time. lispa\amos\core\utilities\StringUtils
replace() Replaces a string with another string inside a larger string, for the first maximum number of values to replace of the search string. lispa\amos\core\utilities\StringUtils
replaceReg() Replaces a string with another string inside a larger string, for the first maximum number of values to replace of the search string. lispa\amos\core\utilities\StringUtils
shortText() Parse string and return limited one lispa\amos\core\utilities\StringUtils
split() Splits the provided text into an array with a maximum length, separators specified. lispa\amos\core\utilities\StringUtils
startsWith() Checks if a string starts with a specified prefix. lispa\amos\core\utilities\StringUtils
strip() Strips any of a set of characters from the start and end of a string. lispa\amos\core\utilities\StringUtils
stripEnd() Strips any of a set of characters from the end of a string. lispa\amos\core\utilities\StringUtils
stripStart() Strips any of a set of characters from the start of a string. lispa\amos\core\utilities\StringUtils
stripToEmpty() Strips whitespace from the start and end of a string returning an empty string if null input. lispa\amos\core\utilities\StringUtils
stripToNull() Strips whitespace from the start and end of a string returning null if the string is empty ('') after the strip. lispa\amos\core\utilities\StringUtils
substring() Gets a substring from the specified string avoiding exceptions. lispa\amos\core\utilities\StringUtils
substringAfter() Gets the substring after the first occurrence of a separator. lispa\amos\core\utilities\StringUtils
substringAfterLast() Gets the substring after the last occurrence of a separator. lispa\amos\core\utilities\StringUtils
substringBefore() Gets the substring before the first occurrence of a separator. lispa\amos\core\utilities\StringUtils
substringBeforeLast() Gets the substring before the last occurrence of a separator. lispa\amos\core\utilities\StringUtils
substringBetween() Gets the string that is nested in between two strings. lispa\amos\core\utilities\StringUtils
trim() Removes control characters (char <= 32) from both ends of a string, handling null by returning null. lispa\amos\core\utilities\StringUtils
trimToEmpty() Removes control characters (char <= 32) from both ends of a string returning an empty string ('') if the string is empty ('') after the trim or if it is null. lispa\amos\core\utilities\StringUtils
trimToNull() Removes control characters (char <= 32) from both ends of a string returning null if the string is empty ('') after the trim or if it is null. lispa\amos\core\utilities\StringUtils
uncapitalize() Uncapitalizes a string changing the first letter to lower case. lispa\amos\core\utilities\StringUtils
upperCase() Converts a string to upper case. lispa\amos\core\utilities\StringUtils

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() {@see StringUtils} instances can NOT be constructed in standard programming. lispa\amos\core\utilities\StringUtils

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
EMPTY_STR '' The empty string ''. lispa\amos\core\utilities\StringUtils
INDEX_NOT_FOUND -1 Represents a failed index search. lispa\amos\core\utilities\StringUtils

Method Details

__construct() protected method

{@see StringUtils} instances can NOT be constructed in standard programming.

Instead, the class should be used as:

StringUtils::trim(' foo ');
protected void __construct ( )
capitalize() public static method

Capitalizes a string changing the first letter to upper case.

No other letters are changed. For a word based algorithm, see {@see capitalize}. A null input string returns null.

StringUtils::capitalize(null);  // null
StringUtils::capitalize('');    // ''
StringUtils::capitalize('cat'); // 'Cat'
StringUtils::capitalize('cAt'); // 'CAt'

See also [[StringUtils::uncapitalize,]] WordUtils::capitalize.

public static string|null capitalize ( $str )
$str string

The string to capitalize.

return string|null

The capitalized string or null if null string input.

charAt() public static method

Returns the character at the specified index.

An index ranges from 0 to StringUtils::length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

public static string charAt ( $str, $index )
$str string

The string to return the character from.

$index integer

The index of the character.

return string

The character at the specified index of the string. The first character is at index 0.

throws \lispa\amos\core\utilities\OutOfBoundsException

If the $index argument is negative or not less than the length of the string.

chomp() public static method

Removes one newline from end of a string if it's there, otherwise leave it alone.

A newline is "\n", "\r", or "\r\n".

StringUtils::chomp(null);          // null
StringUtils::chomp('');            // ''
StringUtils::chomp("abc \r");      // 'abc '
StringUtils::chomp("abc\n");       // 'abc'
StringUtils::chomp("abc\r\n");     // 'abc'
StringUtils::chomp("abc\r\n\r\n"); // "abc\r\n"
StringUtils::chomp("abc\n\r");     // "abc\n"
StringUtils::chomp("abc\n\rabc");  // "abc\n\rabc"
StringUtils::chomp("\r");          // ''
StringUtils::chomp("\n");          // ''
StringUtils::chomp("\r\n");        // ''
public static string chomp ( $str )
$str string

The string to chomp a newline from.

return string

The string $str without newline, null if null string input.

chop() public static method

Remove the specified last character from a string.

If the string ends in \r\n, then remove both of them.

StringUtils::chop(null);       // null
StringUtils::chop('');         // ''
StringUtils::chop("abc \r");   // 'abc '
StringUtils::chop("abc\n");    // 'abc'
StringUtils::chop("abc\r\n");  // 'abc'
StringUtils::chop('abc');      // 'ab'
StringUtils::chop("abc\nabc"); // "abc\nab"
StringUtils::chop('a');        // ''
StringUtils::chop("\r");       // ''
StringUtils::chop("\n");       // ''
StringUtils::chop("\r\n");     // ''
public static string|null chop ( $str )
$str string

The string to chop the last character from.

return string|null

The string without the last character; null if null string input.

compare() public static method

Compares two strings lexicographically.

The comparison is based on the Unicode value of each character in the strings. The character sequence represented by the first string is compared lexicographically to the character sequence represented by the second string. The result is a negative integer if the first string lexicographically precedes the second string. The result is a positive integer if the first string lexicographically follows the second string.

This method returns an integer whose sign is that of calling {@see compare} with normalized versions of the strings.

public static integer compare ( $str1, $str2 )
$str1 string

The first string to be compared.

$str2 string

The second string to be compared.

return integer

A negative integer, zero, or a positive integer as the first string is less than, equal to, or greater than the second string.

compareIgnoreCase() public static method

Compares two strings lexicographically, ignoring case differences.

This method returns an integer whose sign is that of calling {@see compare} with normalized versions of the strings.

public static integer compareIgnoreCase ( $str1, $str2 )
$str1 string

The first string to be compared.

$str2 string

The second string to be compared.

return integer

A negative integer, zero, or a positive integer as the first string is greater than, equal to, or less than the second string, ignoring case considerations.

contains() public static method

public static boolean contains ( $needle, $haystack )
$needle
$haystack
containsWord() public static method

public static boolean containsWord ( $str, $word )
$str
$word
endsWith() public static method

Checks if a string ends with a specified suffix.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils::endsWith(null, null);      // true
StringUtils::endsWith(null, 'def');     // false
StringUtils::endsWith('abcdef', null);  // false
StringUtils::endsWith('abcdef', 'def'); // true
StringUtils::endsWith('ABCDEF', 'def'); // false
StringUtils::endsWith('ABCDEF', 'cde'); // false
public static boolean endsWith ( $str, $suffix )
$str string

The string to check.

$suffix string

The suffix to find.

return boolean

true if the string $str ends with the suffix $suffix, case sensitive, or both null.

equal() public static method

Compares two strings, returning true if they are equal.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils::equals(null, null);   // true
StringUtils::equals(null, 'abc');  // false
StringUtils::equals('abc', null);  // false
StringUtils::equals('abc', 'abc'); // true
StringUtils::equals('abc', 'ABC'); // false
public static boolean equal ( $str1, $str2 )
$str1 string

The first string.

$str2 string

The second string.

return boolean

true if the strings are equal, case sensitive, or both null.

equalsIgnoreCase() public static method

Compares two strings, returning true if they are equal ignoring the case.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case insensitive.

StringUtils::equalsIgnoreCase(null, null);   // true
StringUtils::equalsIgnoreCase(null, 'abc');  // false
StringUtils::equalsIgnoreCase('abc', null);  // false
StringUtils::equalsIgnoreCase('abc', 'abc'); // true
StringUtils::equalsIgnoreCase('abc', 'ABC'); // true
public static boolean equalsIgnoreCase ( $str1, $str2 )
$str1 string

The first string.

$str2 string

The second string.

return boolean

true if the strings are equal, case insensitive, or both null.

indexOf() public static method

Finds the first index within a string from a start position, handling null.

A null or empty ('') string will return -1. A negative start position is treated as zero. A start position greater than the string length returns -1.

StringUtils::indexOf(null, *, *);          // -1
StringUtils::indexOf(*, null, *);          // -1
StringUtils::indexOf('', '', 0);           // 0
StringUtils::indexOf('aabaabaa', 'a', 0);  // 0
StringUtils::indexOf('aabaabaa', 'b', 0);  // 2
StringUtils::indexOf('aabaabaa', 'ab', 0); // 1
StringUtils::indexOf('aabaabaa', 'b', 3);  // 5
StringUtils::indexOf('aabaabaa', 'b', 9);  // -1
StringUtils::indexOf('aabaabaa', 'b', -1); // 2
StringUtils::indexOf('aabaabaa', '', 2);   // 2
StringUtils::indexOf('abc', '', 9);        // 3
public static integer indexOf ( $str, $search, $startPos 0 )
$str string

The string to check.

$search string

The string to find.

$startPos integer

The start position, negative treated as zero.

return integer

The first index of the search character, -1 if no match or null string input.

isBlank() public static method

Checks if a string is whitespace, empty ('') or null.

StringUtils::isBlank(null); // true

StringUtils::isBlank('');        // true
StringUtils::isBlank(' ');       // true
StringUtils::isBlank('bob');     // false
StringUtils::isBlank('  bob  '); // false
public static boolean isBlank ( $str )
$str string

The string to check.

return boolean

true if the string is null, empty or whitespace; false otherwise.

isEmpty() public static method

Checks if a string is empty ('') or null.

StringUtils::isEmpty(null); // true

StringUtils::isEmpty('');        // true
StringUtils::isEmpty(' ');       // false
StringUtils::isEmpty('bob');     // false
StringUtils::isEmpty('  bob  '); // false
public static boolean isEmpty ( $str )
$str string

The string to check.

return boolean

true if the string is empty or null, false otherwise.

isNotBlank() public static method

Checks if a string is not empty (''), not null and not whitespace only.

StringUtils::isNotBlank(null); // false

StringUtils::isNotBlank('');        // false
StringUtils::isNotBlank(' ');       // false
StringUtils::isNotBlank('bob');     // true
StringUtils::isNotBlank('  bob  '); // true
public static boolean isNotBlank ( $str )
$str string

The string to check.

return boolean

true if the string is not empty and not null and not whitespace; false otherwise.

isNotEmpty() public static method

Checks if a string is not empty ('') and not null.

StringUtils::isNotEmpty(null); // false

StringUtils::isNotEmpty('');        // false
StringUtils::isNotEmpty(' ');       // true
StringUtils::isNotEmpty('bob');     // true
StringUtils::isNotEmpty('  bob  '); // true
public static boolean isNotEmpty ( $str )
$str string

The string to check.

return boolean

true if the string is not empty or null, false otherwise.

lastIndexOf() public static method

Finds the first index within a string, handling null.

A null string will return -1. A negative start position returns -1. An empty ('') search string always matches unless the start position is negative. A start position greater than the string length searches the whole string.

StringUtils::lastIndexOf(null, *, *);          // -1
StringUtils::lastIndexOf(*, null, *);          // -1
StringUtils::lastIndexOf('aabaabaa', 'a', 8);  // 7
StringUtils::lastIndexOf('aabaabaa', 'b', 8);  // 5
StringUtils::lastIndexOf('aabaabaa', 'ab', 8); // 4
StringUtils::lastIndexOf('aabaabaa', 'b', 9);  // 5
StringUtils::lastIndexOf('aabaabaa', 'b', -1); // -1
StringUtils::lastIndexOf('aabaabaa', 'a', 0);  // 0
StringUtils::lastIndexOf('aabaabaa', 'b', 0);  // -1
public static integer lastIndexOf ( $str, $search, $startPos 0 )
$str string

The string to check.

$search string

The string to find.

$startPos integer

The start position, negative treated as zero.

return integer

The first index of the search string, -1 if no match or null string input.

length() public static method

Returns the length of a string or 0 if the string is null.

public static integer length ( $str )
$str string

The string to check.

return integer

The length of the string or 0 if the string is null.

lowerCase() public static method

Converts a string to lower case.

A null input string returns null.

StringUtils::lowerCase(null)  = null
StringUtils::lowerCase('')    = ''
StringUtils::lowerCase('aBc') = 'abc'
public static string lowerCase ( $str )
$str string

The string to lower case.

return string

The lower cased string or null if null string input.

removeEnd() public static method

Removes a substring only if it is at the end of a source string, otherwise returns the source string.

A null source string will return null. An empty ('') source string will return the empty string. A null search string will return the source string.

StringUtils::removeEnd(null, *);                    // null
StringUtils::removeEnd('', *);                      // ''
StringUtils::removeEnd(*, null);                    // *
StringUtils::removeEnd('www.domain.com', '.com.');  // 'www.domain.com'
StringUtils::removeEnd('www.domain.com', '.com');   // 'www.domain'
StringUtils::removeEnd('www.domain.com', 'domain'); // 'www.domain.com'
StringUtils::removeEnd('abc', '');                  // 'abc'
public static string|null removeEnd ( $str, $remove )
$str string

The source string to search.

$remove string

The string to search for and remove.

return string|null

The substring with the string removed if found, null if null string input.

removeStart() public static method

Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.

A null source string will return null. An empty ('') source string will return the empty string. A null search string will return the source string.

StringUtils::removeStart(null, *);                    // null
StringUtils::removeStart('', *);                      // ''
StringUtils::removeStart(*, null);                    // *
StringUtils::removeStart('www.domain.com', 'www.');   // 'domain.com'
StringUtils::removeStart('domain.com', 'www.');       // 'domain.com'
StringUtils::removeStart('www.domain.com', 'domain'); // 'www.domain.com'
StringUtils::removeStart('abc', '');                  // 'abc'
public static string|null removeStart ( $str, $remove )
$str string

The source string to search.

$remove string

The string to search for and remove.

return string|null

The substring with the string removed if found, null if null string input.

repeat() public static method

Repeats a string the specified number of times to form a new string, with a specified string injected each time.

StringUtils::repeat(null, 2, null); // null

StringUtils::repeat(null, 2, 'x');  // null
StringUtils::repeat('', 0, null);   // ''
StringUtils::repeat('', 2, '');     // ''
StringUtils::repeat('', 3, 'x');    // 'xxx'
StringUtils::repeat('?', 3, ', ');  // '?, ?, ?'
public static string|null repeat ( $str, $repeat, $separator null )
$str string

The string to repeat.

$repeat integer

The number of times to repeat $str, negative treated as zero.

$separator string

The string to inject.

return string|null

The capitalized string or null if null string input.

replace() public static method

Replaces a string with another string inside a larger string, for the first maximum number of values to replace of the search string.

StringUtils::replace(null, , , *) // null

StringUtils::replace('', *, *, *)           // ''
StringUtils::replace('any', null, *, *)     // 'any'
StringUtils::replace('any', *, null, *)     // 'any'
StringUtils::replace('any', '', *, *)       // 'any'
StringUtils::replace('any', *, *, 0)        // 'any'
StringUtils::replace('abaa', 'a', null, -1) // 'abaa'
StringUtils::replace('abaa', 'a', '', -1)   // 'b'
StringUtils::replace('abaa', 'a', 'z', 0)   // 'abaa'
StringUtils::replace('abaa', 'a', 'z', 1)   // 'zbaa'
StringUtils::replace('abaa', 'a', 'z', 2)   // 'zbza'
StringUtils::replace('abaa', 'a', 'z', -1)  // 'zbzz'
public static string replace ( $text, $search, $replace, $max null )
$text string

The string to search and replace in.

$search string

The string to search for.

$replace string

The string to replace $search with.

$max integer

The maximum number of values to replace, or -1

                    if no maximum.
return string

The text with any replacements processed or null if null string input.

replaceReg() public static method

Replaces a string with another string inside a larger string, for the first maximum number of values to replace of the search string.

StringUtils::replace(null, , , *) // null

StringUtils::replace('', *, *, *)           // ''
StringUtils::replace('any', null, *, *)     // 'any'
StringUtils::replace('any', *, null, *)     // 'any'
StringUtils::replace('any', '', *, *)       // 'any'
StringUtils::replace('any', *, *, 0)        // 'any'
StringUtils::replace('abaa', 'a', null, -1) // 'abaa'
StringUtils::replace('abaa', 'a', '', -1)   // 'b'
StringUtils::replace('abaa', 'a', 'z', 0)   // 'abaa'
StringUtils::replace('abaa', 'a', 'z', 1)   // 'zbaa'
StringUtils::replace('abaa', 'a', 'z', 2)   // 'zbza'
StringUtils::replace('abaa', 'a', 'z', -1)  // 'zbzz'
public static string replaceReg ( $text, $search, $replace, $max = -1 )
$text string

The string to search and replace in.

$search string

The string to search for.

$replace string

The string to replace $search with.

$max integer

The maximum number of values to replace, or -1

                    if no maximum.
return string

The text with any replacements processed or null if null string input.

shortText() public static method

Parse string and return limited one

public static string shortText ( $text, $char_limit )
$text
$char_limit
split() public static method

Splits the provided text into an array with a maximum length, separators specified.

The separator is not included in the returned string array. Adjacent separators are treated as one separator. A null input string returns null. A null $chars splits on whitespace. If more than $max delimited substrings are found, the returned string includes all characters after the first $max - 1 returned strings (including separator characters).

StringUtils::split(null, null, null);      // null
StringUtils::split('', null, null);        // []
StringUtils::split('ab cd ef', null, 0);   // ['ab', 'cd', 'ef']
StringUtils::split('ab   cd ef', null, 0); // ['ab', 'cd', 'ef']
StringUtils::split('ab:cd:ef', ':', 0);    // ['ab', 'cd', 'ef']
StringUtils::split('ab:cd:ef', ':', 2);    // ['ab', 'cd:ef']
public static array|null split ( $str, $chars null, $max 0 )
$str string

The string to parse.

$chars string

The characters used as the delimiters, null splits on whitespace.

$max integer

The maximum number of elements to include in the array. A zero or negative value implies no limit.

return array|null

An array of parsed strings, null if null string input.

startsWith() public static method

Checks if a string starts with a specified prefix.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils::startsWith(null, null);      // true
StringUtils::startsWith(null, 'abc');     // false
StringUtils::startsWith('abcdef', null);  // false
StringUtils::startsWith('abcdef', 'abc'); // true
StringUtils::startsWith('ABCDEF', 'abc'); // false
public static boolean startsWith ( $str, $prefix )
$str string

The string to check.

$prefix string

The prefix to find.

return boolean

true if the string $str starts with the prefix $prefix, case sensitive, or both null.

strip() public static method

Strips any of a set of characters from the start and end of a string.

This is similar to {@see trim} but allows the characters to be stripped to be controlled.

A null input string returns null. An empty string ('') input returns the empty string.

If the string for the characters to remove is null, whitespace is stripped.

StringUtils::strip(null, *);          // null
StringUtils::strip('', *);            // ''
StringUtils::strip('abc', null);      // 'abc'
StringUtils::strip('  abc', null);    // 'abc'
StringUtils::strip('abc  ', null);    // 'abc'
StringUtils::strip(' abc ', null);    // 'abc'
StringUtils::strip('  abcyx', 'xyz'); // '  abc'
public static string|null strip ( $str, $chars )
$str string

The string to remove characters from.

$chars string

The characters to remove. null is treated as whitespace.

return string|null

The stripped string or null if null string input.

stripEnd() public static method

Strips any of a set of characters from the end of a string.

A null input string returns null. An empty string ('') input returns the empty string.

If the string for the characters to remove is null, whitespace is stripped.

StringUtils::stripEnd(null, *)          = null
StringUtils::stripEnd('', *)            = ''
StringUtils::stripEnd('abc', '')        = 'abc'
StringUtils::stripEnd('abc', null)      = 'abc'
StringUtils::stripEnd('  abc', null)    = '  abc'
StringUtils::stripEnd('abc  ', null)    = 'abc'
StringUtils::stripEnd(' abc ', null)    = ' abc'
StringUtils::stripEnd('  abcyx', 'xyz') = '  abc'
public static string|null stripEnd ( $str, $chars )
$str string

The string to remove characters from.

$chars string

The characters to remove. null is treated as whitespace.

return string|null

The stripped string or null if null string input.

stripStart() public static method

Strips any of a set of characters from the start of a string.

A null input string returns null. An empty string ('') input returns the empty string.

If the string for the characters to remove is null, whitespace is stripped.

StringUtils::stripStart(null, *);          // null
StringUtils::stripStart('', *);            // ''
StringUtils::stripStart('abc', '');        // 'abc'
StringUtils::stripStart('abc', null);      // 'abc'
StringUtils::stripStart('  abc', null);    // 'abc'
StringUtils::stripStart('abc  ', null);    // 'abc  '
StringUtils::stripStart(' abc ', null);    // 'abc '
StringUtils::stripStart('yxabc  ', 'xyz'); // 'abc  '
public static string|null stripStart ( $str, $chars )
$str string

The string to remove characters from.

$chars string

The characters to remove. null is treated as whitespace.

return string|null

The stripped string or null if null string input.

stripToEmpty() public static method

Strips whitespace from the start and end of a string returning an empty string if null input.

This is similar to {@see trimToEmpty} but removes whitespace.

StringUtils::stripToEmpty(null);     // ''
StringUtils::stripToEmpty('');       // ''
StringUtils::stripToEmpty('   ');    // ''
StringUtils::stripToEmpty('abc');    // 'abc'
StringUtils::stripToEmpty('  abc');  // 'abc'
StringUtils::stripToEmpty('abc  ');  // 'abc'
StringUtils::stripToEmpty(' abc ');  // 'abc'
StringUtils::stripToEmpty(' ab c '); // 'ab c'
public static string stripToEmpty ( $str )
$str string

The string to be stripped.

return string

The stripped string or an empty string if null input.

stripToNull() public static method

Strips whitespace from the start and end of a string returning null if the string is empty ('') after the strip.

This is similar to {@see trimToNull} but removes whitespace.

StringUtils::stripToNull(null);     // null
StringUtils::stripToNull('');       // null
StringUtils::stripToNull('   ');    // null
StringUtils::stripToNull('abc');    // 'abc'
StringUtils::stripToNull('  abc');  // 'abc'
StringUtils::stripToNull('abc  ');  // 'abc'
StringUtils::stripToNull(' abc ');  // 'abc'
StringUtils::stripToNull(' ab c '); // 'ab c'
public static string|null stripToNull ( $str )
$str string

The string to be stripped.

return string|null

The stripped string or null if whitespace, empty or null string input.

substring() public static method

Gets a substring from the specified string avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the string.

The returned substring starts with the character in the $start position and ends before the $end position. All position counting is zero-based -- i.e., to start at the beginning of the string use $start = 0.

Negative start and end positions can be used to specify offsets relative to the end of the string.

If $start is not strictly to the left of $end, the empty string is returned.

StringUtils::substring(null, *);       // null
StringUtils::substring('', *);         // ''
StringUtils::substring('abc', 0);      // 'abc'
StringUtils::substring('abc', 2);      // 'c'
StringUtils::substring('abc', 4);      // ''
StringUtils::substring('abc', -2);     // 'bc'
StringUtils::substring('abc', -4);     // 'abc'
StringUtils::substring(null, *, *);    // null
StringUtils::substring('', * ,  *);    // '';
StringUtils::substring('abc', 0, 2);   // 'ab'
StringUtils::substring('abc', 2, 0);   // ''
StringUtils::substring('abc', 2, 4);   // 'c'
StringUtils::substring('abc', 4, 6);   // ''
StringUtils::substring('abc', 2, 2);   // ''
StringUtils::substring('abc', -2, -1); // 'b'
StringUtils::substring('abc', -4, 2);  // 'ab'
public static string|null substring ( $str, $start, $end null )
$str string

The string to get the substring from.

$start integer

The position to start from, negative means count back from the end of the string by this many characters.

$end integer

The position to end at (exclusive), negative means count back from the end of the string by this many characters.

return string|null

The substring from start position to end position, null if null string input.

substringAfter() public static method

Gets the substring after the first occurrence of a separator.

The separator is not returned.

A null string input will return null. An empty ('') string input will return the empty string. A null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

StringUtils::substringAfter(null, *);      // null
StringUtils::substringAfter('', *);        // ''
StringUtils::substringAfter(*, null);      // ''
StringUtils::substringAfter('abc', 'a');   // 'bc'
StringUtils::substringAfter('abcba', 'b'); // 'cba'
StringUtils::substringAfter('abc', 'c');   // ''
StringUtils::substringAfter('abc', 'd');   // ''
StringUtils::substringAfter('abc', '');    // 'abc'
public static string|null substringAfter ( $str, $separator )
$str string

The string to get a substring from.

$separator string

The string to search for.

return string|null

The substring after the first occurrence of the separator, null if null string input.

substringAfterLast() public static method

Gets the substring after the last occurrence of a separator.

The separator is not returned.

A null string input will return null. An empty ('') string input will return the empty string. An empty or null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

StringUtils::substringAfterLast(null, *);      // null
StringUtils::substringAfterLast('', *);        // ''
StringUtils::substringAfterLast(*, '');        // ''
StringUtils::substringAfterLast(*, null);      // ''
StringUtils::substringAfterLast('abc', 'a');   // 'bc'
StringUtils::substringAfterLast('abcba', 'b'); // 'a'
StringUtils::substringAfterLast('abc', 'c');   // ''
StringUtils::substringAfterLast('a', 'a');     // ''
StringUtils::substringAfterLast('a', 'z');     // ''
public static string|null substringAfterLast ( $str, $separator )
$str string

The string to get a substring from.

$separator string

The string to search for.

return string|null

The substring after the last occurrence of the separator, null if null string input.

substringBefore() public static method

Gets the substring before the first occurrence of a separator.

The separator is not returned.

A null string input will return null. An empty ('') string input will return the empty string. A null separator will return the input string.

If nothing is found, the string input is returned.

StringUtils::substringBefore(null, *);      // null
StringUtils::substringBefore('', *);        // ''
StringUtils::substringBefore('abc', 'a');   // ''
StringUtils::substringBefore('abcba', 'b'); // 'a'
StringUtils::substringBefore('abc', 'c');   // 'ab'
StringUtils::substringBefore('abc', 'd');   // 'abc'
StringUtils::substringBefore('abc', '');    // ''
StringUtils::substringBefore('abc', null);  // 'abc'
public static string|null substringBefore ( $str, $separator )
$str string

The string to get a substring from.

$separator string

The string to search for.

return string|null

The substring before the first occurrence of the separator, null if null string input.

substringBeforeLast() public static method

Gets the substring before the last occurrence of a separator.

The separator is not returned.

A null string input will return null. An empty ('') string input will return the empty string. An empty or null separator will return the input string.

If nothing is found, the string input is returned.

StringUtils::substringBeforeLast(null, *);      // null
StringUtils::substringBeforeLast('', *);        // ''
StringUtils::substringBeforeLast('abcba', 'b'); // 'abc'
StringUtils::substringBeforeLast('abc', 'c');   // 'ab'
StringUtils::substringBeforeLast('a', 'a');     // ''
StringUtils::substringBeforeLast('a', 'z');     // 'a'
StringUtils::substringBeforeLast('a', null);    // 'a'
StringUtils::substringBeforeLast('a', '');      // 'a'
public static string|null substringBeforeLast ( $str, $separator )
$str string

The string to get a substring from.

$separator string

The string to search for.

return string|null

The substring before the last occurrence of the seperator, null if null string input.

substringBetween() public static method

Gets the string that is nested in between two strings.

Only the first match is returned.

A null input string returns null. A null $open/$close returns null (no match). An empty ('') $open and $close returns an empty string.

StringUtils::substringBetween('wx[b]yz', '[', ']');    // 'b'
StringUtils::substringBetween(null, *, *);             // null
StringUtils::substringBetween(*, null, *);             // null
StringUtils::substringBetween(*, *, null);             // null
StringUtils::substringBetween('', '', '');             // ''
StringUtils::substringBetween('', '', ']');            // null
StringUtils::substringBetween('', '[', ']');           // null
StringUtils::substringBetween('yabcz', '', '');        // ''
StringUtils::substringBetween('yabcz', 'y', 'z');      // 'abc'
StringUtils::substringBetween('yabczyabcz', 'y', 'z'); // 'abc'
public static string|null substringBetween ( $str, $open, $close null )
$str string

The string containing the substrings, null returns null, empty returns empty.

$open string

The string identifying the start of the substring, empty returns null.

$close string

The string identifying the end of the substring, empty returns null.

return string|null

The string after the substring, null if no match.

trim() public static method

Removes control characters (char <= 32) from both ends of a string, handling null by returning null.

This method removes start and end characters <= 32. To strip whitespace use {@see strip}.

To trim your choice of characters, use the {@see strip} method.

StringUtils::trim(null);          // null
StringUtils::trim('');            // ''
StringUtils::trim('     ');       // ''
StringUtils::trim('abc');         // 'abc'
StringUtils::trim('    abc    '); // 'abc'
public static string trim ( $str )
$str string

The string to be trimmed.

return string

The trimmed string or null if null string input.

trimToEmpty() public static method

Removes control characters (char <= 32) from both ends of a string returning an empty string ('') if the string is empty ('') after the trim or if it is null.

This method removes start and end characters <= 32. To strip whitespace use {@see stripToEmpty}.

StringUtils::trimToEmpty(null);          // ''
StringUtils::trimToEmpty('');            // ''
StringUtils::trimToEmpty('     ');       // ''
StringUtils::trimToEmpty('abc');         // 'abc'
StringUtils::trimToEmpty('    abc    '); // 'abc'
public static string trimToEmpty ( $str )
$str string

The string to be trimmed.

return string

The trimmed string or an empty string if null input.

trimToNull() public static method

Removes control characters (char <= 32) from both ends of a string returning null if the string is empty ('') after the trim or if it is null.

This method removes start and end characters <= 32. To strip whitespace use {@see stripToNull}.

StringUtils::trimToNull(null);          // null
StringUtils::trimToNull('');            // null
StringUtils::trimToNull('     ');       // null
StringUtils::trimToNull('abc');         // 'abc'
StringUtils::trimToNull('    abc    '); // 'abc'
public static string|null trimToNull ( $str )
$str string

The string to be trimmed.

return string|null

The trimmed string' or null if only chars &lt;= 32, empty or null string` input.

uncapitalize() public static method

Uncapitalizes a string changing the first letter to lower case.

No other letters are changed. For a word based algorithm, see {@see uncapitalize}. A null input string returns null.

StringUtils::uncapitalize(null);  // null
StringUtils::uncapitalize('');    // ''
StringUtils::uncapitalize('Cat'); // 'cat'
StringUtils::uncapitalize('CAT'); // 'cAT'

See also [[StringUtils::capitalize,]] WordUtils::uncapitalize.

public static string|null uncapitalize ( $str )
$str string

The string to uncapitalize.

return string|null

The uncapitalized string or null if null string input.

upperCase() public static method

Converts a string to upper case.

A null input string returns null.

StringUtils::upperCase(null)  = null
StringUtils::upperCase('')    = ''
StringUtils::upperCase('aBc') = 'ABC'
public static string upperCase ( $str )
$str string

The string to upper case.

return string

The upper cased string or null if null string input.