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

Measure similarity between two strings

similar_text() returns the number of matching characters and can also write a similarity percentage to a third argument. It is useful for small, byte-oriented comparisons, not language-aware search. Its execution cost can be high on long strings.

$first = 'Plus2net PHP';$second = 'Plus2net Python';$matches = similar_text($first, $second, $percent);echo round($percent, 1); // Display the percentage to one decimal place
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

similar_text(string $string1, string $string2, float &$percent = null): int
$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. When supplied, the by-reference third parameter receives the percentage; the function itself still returns an integer count.

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); // 2 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";$matches = similar_text($str1, $str2, $percent);echo "Matching characters: " . $matches . PHP_EOL;echo "Similarity: " . round($percent, 2) . "%";
Output:
Matching characters: 2
Similarity: 33.33%

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";$matches = similar_text($str1, $str2, $percent);echo "Matching characters: " . $matches . PHP_EOL;echo "Similarity: " . round($percent, 2) . "%";
Output:
Matching characters: 4
Similarity: 100%

Conclusion

Use similar_text() when you need a simple, case-sensitive character-similarity measure for relatively short strings. 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



plus2net.com











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