Methods
Compares two strings and gives the number of character replacements that must be performed to convert one
of the strings into the other. A very useful method to use in fuzzy text searches where we want to look for
similar texts. This method uses the Levenshtein method for the comparison:
compareByLevenshtein(string $string1,string $string2): \org\turbocommons\src\main\php\utils\number
static
The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete
to transform string1 into string2. The complexity of the algorithm is O(m*n), where n and m are the length
of string1 and string2.
| example
|
and "aba" will output 1 cause we need to change the h for a b to transform one string into another. |
Arguments
$string1
stringThe first string to compare
$string2
stringThe second string to compare
Response
\org\turbocommons\src\main\php\utils\numberThe number of characters to replace to convert $string1 into $string2 where 0 means both strings are the same.
The higher the result, the more different the strings are.
Compares the percentage of similarity between two strings, based on the Levenshtein method. A very useful method
to use in fuzzy text searches where we want to look for similar texts.
compareSimilarityPercent(string $string1,string $string2): \org\turbocommons\src\main\php\utils\number
static
Arguments
$string1
stringThe first string to compare
$string2
stringThe second string to compare
Response
\org\turbocommons\src\main\php\utils\numberA number between 0 and 100, being 100 if both strings are the same and 0 if both strings are totally different
Count the number of characters that match the given letter case on the given string
countByCase(string $string,string $letterCase = self::FORMAT_ALL_UPPER_CASE): integer
static
Arguments
$string
stringThe string which case matching characters will be counted
$letterCase
stringDefines which letter case are we looking for: StringUtils::FORMAT_ALL_UPPER_CASE or
StringUtils::FORMAT_ALL_LOWER_CASE
Response
integerThe number of characters with the specified letter case that are present on the string
Given a string with a list of elements separated by '/' or '\' that represent some arbitrary path structure,
this method will return the number of elements that are listed on the path.
countPathElements(string $path): \org\turbocommons\src\main\php\utils\number
static
| example
|
-> results in 1
"//folder/folder2/folder3/file.txt" -> results in 4 |
Arguments
$path
stringA string containing some arbitrary path.
Response
\org\turbocommons\src\main\php\utils\numberThe number of elements that are listed on the provided path
Count the number of times a string is found inside another string
countStringOccurences(string $string,string $findMe): integer
static
Arguments
$string
stringThe string where we want to search
$findMe
stringThe string that we want to look for
Response
integerThe number of times that $findMe appears on $string
Count the number of words that exist on the given string
countWords(string $string,string $wordSeparator = ' '): integer
static
Arguments
$string
stringThe string which words will be counted
$wordSeparator
string' ' by default. The character that is considered as the word sepparator
Response
integerThe number of words (elements divided by the wordSeparator value) that are present on the string
Find the string that is most similar to a provided one inside an array of strings.
findMostSimilarString(string $string,array $listOfStrings): string
static
NOTE: The strings are compared by using the Levenshtein method.
| see
|
\org\turbocommons\src\main\php\utils\StringUtils::compareByLevenshtein |
Arguments
$string
stringThe string that we want to compare with all of the provided array
$listOfStrings
arrayAn array of strings with all the strings we want to compare
Response
stringThe string that was found to be more similar to the provided one
Find the array index that contains the string that is most similar to a provided one inside an array of strings.
findMostSimilarStringIndex(string $string,array $listOfStrings): integer
static
NOTE: The strings are compared by using the Levenshtein method.
| see
|
\org\turbocommons\src\main\php\utils\StringUtils::compareByLevenshtein |
Arguments
$string
stringThe string that we want to compare with all of the provided array
$listOfStrings
arrayAn array of strings with all the strings we want to compare
Response
integerThe array index for the string that was found to be more similar to the provided one
Strictly check that the provided value is a non empty string or throw an exception
forceNonEmptyString(mixed $value,string $valueName = '',string $errorMessage = 'must be a non empty string'): void
static
Uses the same criteria as the self::isEmpty() method
| Throws |
- \InvalidArgumentException
If the check fails
|
Arguments
$value
mixedA value to check
$valueName
stringThe name of the value to be shown at the beginning of the exception message
$errorMessage
stringThe rest of the exception message
Strictly check that the provided value is a string or throw an exception
forceString(mixed $value,string $valueName = '',string $errorMessage = 'must be a string'): void
static
| Throws |
- \InvalidArgumentException
If the check fails
|
Arguments
$value
mixedA value to check
$valueName
stringThe name of the value to be shown at the beginning of the exception message
$errorMessage
stringThe rest of the exception message
Full text search is the official name for the process of searching on a big text content based on a string containing some text to find.
formatForFullTextSearch(string $string,string $wordSeparator = ' '): string
static
This method will process a text so it removes all the accents and non alphanumerical characters that are not usefull for searching on strings,
convert everything to lower case and remove empty spaces.
To perform the search it is important that both search and searched strings are standarized the same way, to maximize possible matches.
Arguments
$string
stringString to process
$wordSeparator
stringThe character that will be treated as the word separator. By default it is the empty space character ' '
Response
stringThe resulting string
Generate a path string from an array of elements.
generatePath(array $elements,string $separator = ''): string
static
Arguments
$elements
arrayAn array of elements that will be used to form the path.
$separator
stringAn optional separator that will be used to join the elements in the path. Defaults to DIRECTORY_SEPARATOR.
Response
stringA string representing the path formed from the given elements.
Generates a random string with the specified length and options
generateRandom(integer $minLength,integer $maxLength,array $charSet = array('0-9', 'a-z', 'A-Z')): string
static
Arguments
$minLength
integerSpecify the minimum possible length for the generated string
$maxLength
integerSpecify the maximum possible length for the generated string
$charSet
arrayDefines the list of possible characters to be generated. Each element of charSet must be a string containing
the possible characters like 'a1kjuhAO' or a range like 'a-z', 'A-D', '0-5', ... etc.
Note that - character must be escaped - when not specified as part of a range
Default charset is alphanumeric ['0-9', 'a-z', 'A-Z']
Response
stringA randomly generated string
TODO
getDomainFromUrl( $string)
static
TODO
getHostNameFromUrl( $string)
static
Generates an array with a list of common words from the specified text.
getKeyWords(string $string,string $max = 25,string $longerThan = 3,string $shorterThan = 15,string $ignoreNumericWords = false): array
static
The list will be sorted so the words that appear more times on the string are placed first.
Arguments
$string
stringPiece of text that contains the keywords we want to extract
$max
stringThe maxium number of keywords that will appear on the result. If set to null, all unique words on the given text will be returned
$longerThan
stringThe minimum number of chars for the keywords to find. This is useful to filter some irrelevant words like: the, it, me, ...
$shorterThan
stringThe maximum number of chars for the keywords to find
$ignoreNumericWords
stringTells the method to skip words that represent numeric values on the result. False by default
Response
arrayThe list of keywords that have been extracted from the given text
Extracts all the lines from the given string and outputs an array with each line as an element.
getLines(string $string,array $filters = array('/\s+/')): array
static
It does not matter which line separator's been used (windows: \r\n, Linux/Unix: \n, Mac: \r). All source lines will be correctly extracted.
Arguments
$string
stringText containing one or more lines that will be converted to an array with each line on a different element.
$filters
arrayOne or more regular expressions that will be used to filter unwanted lines. Lines that match any of the
filters will be excluded from the result. By default, all empty lines are ignored (those containing only newline, blank, tabulators, etc..).
Response
arrayA list with all the string lines sepparated as different array elements.
Given a string with a list of elements separated by '/' or '\' that represent some arbitrary path structure,
this method will format the specified path and remove the number of requested path elements (from its right
side) and return the path without that elements.
getPath(string $path,integer $elementsToRemove = 1,string $separator = '/'): string
static
This method can be used with Operating system file paths, urls, or any other string that uses the 'slash separated'
format to encode a path.
| example
|
-> results in "/folder/folder2/folder3" if elementsToRemove = 1
"//folder/folder2\folder3\file.txt" -> results in "/folder/folder2" if elementsToRemove = 2 |
| see
|
\org\turbocommons\src\main\php\utils\StringUtils::formatPath |
Arguments
$path
stringA string containing some arbitrary path.
$elementsToRemove
integer(one by default) The number of elements that we want to remove from the right side of the path.
$separator
stringThe character to use as the element divider for the returned path. Only slash '/' or backslash '\' are allowed.
Response
stringThe received path without the specified number of elements and correctly formatted
Given a string with a list of elements separated by '/' or '\' that represent some arbitrary path structure,
this method will return the element that is located at the requested position. If no position is defined,
by default the last element of the path will be returned (the most to the right one).
getPathElement(string $path,integer $position = -1): string
static
This method can be used with Operating system file paths, urls, or any other string that uses the 'slash separated'
format to encode a path.
| example
|
-> results in "file.txt" if (-1) position is defined
"//folder/folder2\folder3\file.txt" -> results in "folder" if position 0 is defined
"//folder/folder2\folder3\file.txt" -> results in "folder3" if position 2 is defined
"//folder/folder2\folder3\file.txt" -> results in "folder3" if position -2 is defined
"//folder/folder2\folder3\file.txt" -> results in "folder2" if position -3 is defined |
Arguments
$path
stringA string containing some arbitrary path.
$position
integerThe index for the element that we want to extract from the path. Positive values will get path elements
starting from the left side, being 0 the first most to the left one. Negative values will get path elements starting from
the right side, being -1 the last path element (or the first most to the right one).
If not specified, the last one will be returned.
Response
stringThe element at the specified path position or the last one if no position is defined
This method works in the same way as getPathElement but it also removes the extension part from the result
if it has any.
getPathElementWithoutExt(string $path,integer $position = -1,string $extensionSeparator = '.'): string
static
| example
|
-> results in "file" if position = -1. Notice that ".txt" extension is removed
"//folder/folder2\folder3\file.txt" -> results in "folder3" if position = 2. "folder3" has no extension so it does not get modified. |
| see
|
\org\turbocommons\src\main\php\utils\StringUtils::getPathElement |
Arguments
$path
stringA string containing some arbitrary path.
$position
integerThe index for the element that we want to extract from the path. If not specified, the
last one will be returned.
$extensionSeparator
stringThe character to be used as the extension separator. The most commonly used is '.'
Response
stringThe element at the specified path position with it's extension removed or the last one if no position is defined
This method works in the same way as getPathElement but it only gives the element extension if it has any.
getPathExtension(string $path,integer $position = -1,string $extensionSeparator = '.'): string
static
| example
|
-> results in "txt" if position = -1. Notice that extension without separator character is returned
"//folder/folder2\folder3\file.txt" -> results in "folder3" if position = 2. "folder3" has no extension so it does not get modified. |
| see
|
\org\turbocommons\src\main\php\utils\StringUtils::getPathElement |
Arguments
$path
stringA string containing some arbitrary path.
$position
integerThe index for the element extension that we want to extract from the path. If not specified, the
last one will be returned.
$extensionSeparator
stringThe character to be used as the extension separator. The most commonly used is '.'
Response
stringThe extension from the element at the specified path position or the extension from the last one if no position is defined
TODO - translate from Ts
getSchemeFromUrl()
static
Test if a given string is written using the camel case format or not.
isCamelCase(string $string,string $type = self::FORMAT_CAMEL_CASE): boolean
static
3 variants can be checked: Default one that does not care about the first letter case, and Upper or Lower camel case formats which
force it to be upper case and lower case respectively.
Arguments
$string
stringThe string to be tested
$type
stringThe variant of camel case we are testing: StringUtils::FORMAT_UPPER_CAMEL_CASE, StringUtils::FORMAT_LOWER_CAMEL_CASE or StringUtils::FORMAT_CAMEL_CASE (default).
Response
booleanTrue if the given string is accepted as camel case for the specified variant.
Tells if a specified string is semantically empty, which applies to any string that is comprised of empty spaces, new line characters, tabulations or any other
characters without a visually semantic value to the user.
isEmpty(string $string,array $emptyChars = array()): boolean
static
Example1: Following strings are considered as empty: " ", "", " \n\n\n", " \t\t\n"
Example2: Following strings are not considered as empty: "hello", " a", " \n\nB"
Arguments
$string
stringThe text to check
$emptyChars
arrayCustom list of strings that will be also considered as empty characters. For example, we can define 'NULL' and '' as empty string values by setting this to ['NULL', '']
Response
booleanfalse if the string is not empty, true if the string contains non semantically valuable characters or any other characters defined as "empty" values
Checks if a given string ends with any of the specified values.
isEndingWith(string $str,array $endingValues = array()): boolean
static
Arguments
$str
stringThe string to check
$endingValues
arrayAn array of strings that the input string should end with.
Response
booleanTrue if the input string ends with any of the specified values, False otherwise.
Test if a given string is written using the snake case format or not.
isSnakeCase(string $string,string $type = self::FORMAT_SNAKE_CASE): boolean
static
3 variants can be checked: Default one that does not care about the text case, and Upper or Lower snake case formats which
force it to be upper case and lower case respectively.
Arguments
$string
stringThe string to be tested
$type
stringThe variant of snake case we are testing: StringUtils::FORMAT_UPPER_SNAKE_CASE, StringUtils::FORMAT_LOWER_SNAKE_CASE or StringUtils::FORMAT_SNAKE_CASE (default).
Response
booleanTrue if the given string is accepted as snake case for the specified variant.
Checks if the given string starts with any of the specified values.
isStartingWith(string $str,array $startingValues = array()): boolean
static
Arguments
$str
stringThe string to check
$startingValues
arrayAn array of strings that the input string should start with.
Response
booleanTrue if the input string starts with any of the specified values, False otherwise.
Tells if the given value is a string or not
isString(mixed $value): boolean
static
Arguments
$value
mixedA value to check
Response
booleantrue if the given value is a string, false otherwise
Tells if the given string is a valid url or not
isUrl(mixed $value): boolean
static
Arguments
$value
mixedThe value to check
Response
booleanFalse in case the validation fails or true if validation succeeds.
Method that limits the length of a string and optionally appends informative characters like ' .
limitLen(string $string,integer $limit = 100,string $limiterString = ' ...'): string
static
..'
to inform that the original string was longer.
Arguments
$string
stringString to limit
$limit
integerMax number of characters
$limiterString
stringIf the specified text exceeds the specified limit, the value of this parameter will be added to the end of the result. The value is ' ...' by default.
Response
stringThe specified string but limited in length if necessary. Final result will never exceed the specified limit, also with the limiterString appended.
Pad a string to a certain length with another string
pad(string $string,integer $padLength,string $padString = '0',string $mode = 'LEFT'): string
static
Arguments
$string
stringThe string to which we want to fill the empty spaces
$padLength
integerThe minimum length that we want for the resulting string to have
$padString
stringThe character or characters which we want to add to the string to match the target length
$mode
stringLEFT to append the padString to the left of the string, RIGHT to append the padString to the right of the string
Response
stringThe padded string
Converts all accent characters to ASCII characters on a given string.<br>
This method is based on the WordPress implementation called remove_Accents
removeAccents(string $string): string
static
Arguments
$string
stringText from which accents must be cleaned
Response
stringThe given string with all accent and diacritics replaced by the respective ASCII characters.
Remove all html code and tags from the specified text, so it gets converted to plain text.
removeHtmlCode(string $string,string $allowedTags = ''): string
static
Arguments
$string
stringThe string to process
$allowedTags
stringYou can use this optional second parameter to specify tags which should not be stripped. Example: '<p><a><b><li><br><u>' To preserve the specified tags
Response
stringThe string without the html code
Deletes all new line characters from the given string
removeNewLineCharacters(string $string): string
static
Arguments
$string
stringThe string to process
Response
stringThe string without any new line character
Remove all duplicate consecutive fragments from the provided string and leave only one occurence
removeSameConsecutive(string $string,array $set = array()): string
static
| example
|
we want to remove all duplicate consecutive empty spaces,
we will call removeSameConsecutive('string', [' ']) we want to remove all duplicate consecutive new line characters,
we will call removeSameConsecutive("string\n\n\nstring", ["\n"]) we want to remove all duplicate "hello" words, we will call
removeSameConsecutive('hellohellohellohello', ['hello']) |
Arguments
$string
stringThe string to process
$set
arrayA list with the fragments that will be removed when found consecutive. If this value is
an empty array, all duplicate consecutive characters will be deleted.
We can pass here words or special characters like "\n"
Response
stringThe string with a maximum of one consecutive sequence for all those matching the provided set
Remove all urls from The string to process
removeUrls(string $string,string $replacement = 'xxxx'): string
static
Arguments
$string
stringThe string to process
$replacement
stringThe replacement string that will be shown when some url is removed
Response
stringThe string without the urls
Deletes all the words that are longer than the specified length from the provided string
removeWordsLongerThan(string $string,integer $longerThan = 3,string $wordSeparator = ' '): string
static
Arguments
$string
stringThe string to process
$longerThan
integerThe maximum length for the words to be preserved. Any word that exceeds the specified length will be removed from the string.
$wordSeparator
stringThe character that will be used as the word separator. By default it is the empty space character ' '
Response
stringThe string without the removed words
Deletes all the words that are shorter than the specified length from the provided string
removeWordsShorterThan(string $string,integer $shorterThan = 3,string $wordSeparator = ' '): string
static
Arguments
$string
stringThe string to process
$shorterThan
integerThe minimum length for the words to be preserved. So any word that is shorther than the specified value will be removed.
$wordSeparator
stringThe character that will be used as the word separator. By default it is the empty space character ' '
Response
stringThe string without the removed words
TODO docs
TODO Verify that this version works exactly the same as the TS one, and implement the same unit tests
replace( $string, $search, $replacement, $count = -1)
static
TODO translate from TS
replaceMulti()
static
Remove whitespaces (or any custom set of characters) from both sides of a string
trim(string $string,string $characters = " \n\r"): string
static
| example
|
StringUtils::trim("abcXXabc", "abc") outputs "XX" |
Arguments
$string
stringA string to process
$characters
stringA set of characters that will be trimmed from both string sides. By default,
empty space and new line characters are defined : " \n\r"
Response
stringThe trimmed string
Remove whitespaces (or any custom set of characters) from a string left side
trimLeft(string $string,string $characters = " \n\r"): string
static
| example
|
StringUtils::trimLeft("abcXXabc", "abc") outputs "XXabc" |
Arguments
$string
stringA string to process
$characters
stringA set of characters that will be trimmed from string left side. By default,
empty space and new line characters are defined : " \n\r"
Response
stringThe trimmed string
Remove whitespaces (or any custom set of characters) from a string right side
trimRight(string $string,string $characters = " \n\r"): string
static
| example
|
StringUtils.trimRight("abcXXabc", "abc") outputs "abcXX" |
Arguments
$string
stringA string to process
$characters
stringA set of characters that will be trimmed from string right side. By default,
empty space and new line characters are defined : " \n\r"
Response
stringThe trimmed string
Constants