\TransvisionStrings

Strings class

This class is for all the methods we need to manipulate strings

Summary

Methods
Properties
Constants
mtrim()
startsWith()
endsWith()
inString()
multipleStringReplace()
highlightSpecial()
highlightString()
markString()
getLength()
getSimilar()
levenshteinUTF8()
levenshteinQuality()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

mtrim()

mtrim(string  $string) : string

Replace contiguous spaces in a string by a single space.

Leading and trailing spaces are preserved but collapsed to a single space.

Parameters

string $string

The string to analyze

Returns

string —

Cleaned up string with extra spaces merged

startsWith()

startsWith(string  $haystack, array  $needles) : boolean

Check if $haystack starts with a string in $needles.

$needles can be a string or an array of strings.

Parameters

string $haystack

String to analyse

array $needles

The string to look for

Returns

boolean —

True if the $haystack string starts with a string in $needles

endsWith()

endsWith(string  $haystack, array  $needles) : boolean

Check if $haystack ends with a string in $needles.

$needles can be a string or an array of strings.

Parameters

string $haystack

String to analyse

array $needles

The strings to look for

Returns

boolean —

True if the $haystack string ends with a string in $needles

inString()

inString(string  $haystack, mixed  $needles, boolean  $match_all = false) : boolean

Check if $needles are in $haystack

Parameters

string $haystack

String to analyze

mixed $needles

The string (or array of strings) to look for

boolean $match_all

True if we need to match all $needles, false if it's enough to match one. Default: false

Returns

boolean —

True if the $haystack string contains any/all $needles

multipleStringReplace()

multipleStringReplace(array  $replacements, string  $string) : string

Returns a string after replacing all the items provided in an array

Parameters

array $replacements

List of replacements to do as : ['before1' => 'after1', 'before2' => 'after2']

string $string

The string to process

Returns

string —

Processed string

highlightSpecial()

highlightSpecial(string  $string, boolean  $exclude_whitespaces = true) : string

Highlight special characters in the string

Parameters

string $string

Source text

boolean $exclude_whitespaces

Optional param to specify if we need to highlight white spaces. White spaces are not highlighted by default.

Returns

string —

Same string with specific sub-strings in elements for styling with CSS

highlightString()

highlightString(string  $str) : string

Highlight searched terms in a string previously marked by markString()

Parameters

string $str

String with marked items to highlight

Returns

string —

HTML with searched terms in . Returns the original string if it's missing.

markString()

markString(string  $needle, string  $haystack) : string

This method surrounds a searched term with ←→ It's used together with highlightString() to replace these with spans.

Parameters

string $needle

The term we when to find and mark for highlighting

string $haystack

The string we search in

Returns

string —

The original string with the searched term surronded by arrows. Returns the original string if it's missing.

getLength()

getLength(string  $str) : integer

Get multibyte UTF-8 string length, html tags stripped

Parameters

string $str

A multibyte string

Returns

integer —

The length of the string after removing all html

getSimilar()

getSimilar(string  $needle, array  $haystack, integer  $number = 1) : array

Search for similar strings in an array

Parameters

string $needle

string to search for

array $haystack

array of strings to search into

integer $number

optional, number of results we want, defaults to 1

Returns

array —

Closest strings to $needle in $haystack or empty array if no match

levenshteinUTF8()

levenshteinUTF8(string  $string1, string  $string2) : integer

Levenshtein implementation that works with unicode strings It calculates the number of insertions/deletions/Additions needed to change string 1 into string 2

Fallback to native levenshtein() if strings are ascii

Parameters

string $string1

First string to compare

string $string2

Second string to compare

Returns

integer —

The Levenshtein distance

levenshteinQuality()

levenshteinQuality(string  $string1, string  $string2) : float

Get a quality index (%) for two strings compared with Levenshtein distance

Parameters

string $string1

First string to compare

string $string2

Second string to compare

Returns

float —

String similarity as a percent, higher is better