Core PHP

String Comparison Functions

String Comparison Functions
levenshtein()
integer levenshtein(string $sString1, string $sString2, integer $iInsertCost = 1, integer $iReplaceCost = 1, integer $iDeleteCost = 1)
This function returns the Levenshtein distance between $sString1 and $sString2. This distance is the number of insertions, replacements, and deletions multiplied by the cost of each.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
integer $iInsertCost This is the cost of an insertion.
integer $iReplaceCost This is the cost of a replacement.
integer $iDeleteCost This is the cost of a deletion.
similar_text()
integer similar_text(string $sString1, string $sString2, double &$drPercentSimilar = 0)
This function returns the number of matching characters in $sString1 and $sString2. If a double is passed in as the third parameter, it is assigned the percentage similarity of the strings. The order that the strings are passed in matters.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
double &$drPercentSimilar This returns the percentage similarity of the two strings.
strcasecmp()
integer strcasecmp(string $sString1, string $sString2)
This function returns a number indicating how $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is not case-sensitive.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
strcmp()
integer strcmp(string $sString1, string $sString2)
This function returns a number indicating how $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is case-sensitive.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
strcoll()
integer strcoll(string $sString1, string $sString2)
This function returns a number indicating how $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is case-sensitive and based on the locale.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
strnatcasecmp()
integer strnatcasecmp(string $sString1, string $sString2)
This function returns a number indicating how $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is not case-sensitive and based on natural ordering.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
strnatcmp()
integer strnatcmp(string $sString1, string $sString2)
This function returns a number indicating how $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is case-sensitive and based on natural ordering.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
strncasecmp()
integer strncasecmp(string $sString1, string $sString2, integer $iCompareLength)
This function returns a number indicating how the first $iCompareLength characters of $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is not case-sensitive.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
integer $iCompareLength This is the length of characters that are to be compared.
strncmp()
integer strncmp(string $sString1, string $sString2, integer $iCompareLength)
This function returns a number indicating how the first $iCompareLength characters of $sString1 and $sString2 compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is case-sensitive.
Argument Description
string $sString1 This is the first string that will be compared.
string $sString2 This is the second string that will be compared.
integer $iCompareLength This is the length of characters that are to be compared.
substr_compare()
integer substr_compare(string $sFull, string $sSub, integer $iStart, integer $iLength = infinite, bool $bIgnoreCase = false)
This function returns a number indicating how the substring of $sFull starting at $iStart and running over $iLength commpares to $sSub compare alphabetically:
  • < 0 ⇒ $sString1 < $sString2
  • = 0 ⇒ $sString1 = $sString2
  • > 0 ⇒ $sString1 > $sString2
This comparison is case-sensitive if the boolean $bIgnoreCase is false.
Argument Description
string $sFull This is the string that we will take a substring of for the comparison.
string $sSub This is the second string that will be compared in its entirety.
integer $iStart This is the starting location of the substring in $sFull
integer $iLength This is the length of the substring in $sFull
bool $bIgnoreCase This boolean determines whether we should consider the case in the comparison.
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.