similar_text(): Comparing strings to get the percentage of similarity

The PHP similar_text() function compares two strings and calculates the percentage of similarity between them. This function is useful for tasks like fuzzy matching, where you need to compare how closely two strings resemble each other.

Syntax

int similar_text ( string $first , string $second [, float &$percent ] )
$first: The first string to compare.
$second: The second string to compare.
$percent (optional): A variable passed by reference that holds the percentage of similarity between the two strings.
Return Value: The function returns the number of matching characters between the two strings. If the third parameter is provided, it also returns the percentage of similarity.

Basic Example of similar_text()

Here, we compare two strings to find the number of matching characters.
$str1 = "apple";
$str2 = "apricot";
echo similar_text($str1, $str2); // Returns the number of matching characters
Output:
2

Example with Percentage Calculation

In this example, we calculate both the number of matching characters and the percentage of similarity between two strings.
$str1 = "apple";
$str2 = "apricot";
similar_text($str1, $str2, $percent);
echo "Matching characters: " . similar_text($str1, $str2);
echo "<BR>Similarity: " . $percent . "%";
Output:
Matching characters: 2
Similarity: 33.333333333333%

Example with Completely Different Strings

This example shows how similar_text() behaves when comparing completely different strings.
$str1 = "dog";
$str2 = "cat";
echo "Matching characters: " . similar_text($str1, $str2);
Output:
Matching characters: 0

Case Sensitivity Example

The similar_text() function is case-sensitive. This example demonstrates how case differences impact the result.
$str1 = "Hello";
$str2 = "hello";
echo "Matching characters: " . similar_text($str1, $str2);
Output:
Matching characters: 4

Example with Identical Strings

If the two strings are identical, the percentage of similarity will be 100%.
$str1 = "test";
$str2 = "test";
similar_text($str1, $str2, $percent);
echo "Matching characters: " . similar_text($str1, $str2);
echo "<BR>Similarity: " . $percent . "%";
Output:
Matching characters: 4
Similarity: 100%

Conclusion

The PHP similar_text() function is highly useful for comparing strings and finding how similar they are. It works well for fuzzy matching and string comparison, and by using the optional percentage parameter, you can easily quantify the level of similarity between two strings.
String Functions strcasecmp() : Case-insensitive string comparison strcmp() : Case-sensitive string comparison
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer