PHP: similar_text() function
Description
The similar_text() function is used to calculate the number of similar characters between two strings.
The similar_text() function can also calculate the similarity of the strings in percent.
Version:
(PHP 4 and above)
Syntax:
similar_text(first_string, second_string, npercent)
Parameter:
Name | Description | Required / Optional |
Type |
---|---|---|---|
first_string | The first input string. | Required | String |
second_string | The second input string. | Required | String |
npercent | Variable to store the percentage of similarity between the input strings. | Optional | Integer |
Return value:
Returns the number of matching chars in both strings.
Value Type: Integer
Example:
<?php
echo "Comparing strings : 'Good Morning' and 'Good Evening'".'<br>';
echo 'No of similar characters : '.similar_text('Good Morning', 'Good Evening').'<br>';
similar_text('Good Morning', 'Good Evening', $similar_percent);
echo 'Similarity between two strings in percentage : '. $similar_percent;
?>
Output:
Comparing strings: 'Good Morning' and 'Good Evening' No of similar characters : 9 Similarity between two strings in percentage : 75
View the example in the browser
See also
PHP: Tips of the Day
PHP: Remove first 4 characters of a string with PHP
You could use the substr function to return a substring starting from the 5th character:
$str = "The quick brown fox jumps over the lazy dog." $str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."
Ref : https://bit.ly/2LJDLQx
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework