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.
$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.