Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.
Hello Friends Today, through this tutorial, I will tell you How to Use `similar_text()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. The `similar_text()` function in PHP is used to calculate the similarity between two strings by comparing how many characters they have in common. It returns the number of matching characters between two strings. Here’s how you can use it with examples:
<?php // Example 1: Basic usage of similar_text() $string1 = "apple"; $string2 = "april"; similar_text($string1, $string2, $percentage); echo "Similarity between '$string1' and '$string2' is: $percentage%\n"; // Example 2: Using similar_text() to find similarity between two longer strings $string3 = "hello world"; $string4 = "hello there"; similar_text($string3, $string4, $percentage); echo "Similarity between '$string3' and '$string4' is: $percentage%\n"; ?>
Output:
Similarity between 'apple' and 'april' is: 60% Similarity between 'hello world' and 'hello there' is: 70%
In these examples, we calculate the similarity between two pairs of strings (`$string1` and `$string2`, and `$string3` and `$string4`) using the `similar_text()` function. The similarity percentage is stored in the variable `$percentage`, which is then echoed to the output.
Remember, the similarity percentage indicates how much the two strings match, with 100% indicating that the strings are identical.