PHP similar_text() Function
In this chapter you will learn:
- Definition for PHP similar_text() Function
- Syntax for PHP similar_text() Function
- Parameter for PHP similar_text() Function
- Return for PHP similar_text() Function
- Example - Calculate the similarity between two strings and return the matching characters
- Example - Calculate the similarity between two strings in percent
Definition
The similar_text() function calculates the similarity between two strings.
Syntax
PHP similar_text() Function has the following syntax.
similar_text(string1,string2,percent)
Parameter
Parameter | Is Required | Description |
---|---|---|
string1 | Required. | First string to be compared |
string2 | Required. | Second string to be compared |
percent | Optional. | Variable name for storing the similarity in percent |
Return
PHP similar_text() Function returns the number of matching characters of two strings.
Example
Calculate the similarity between two strings and return the matching characters:
<?php
echo similar_text("Hello World","Hello from java2s.com");
?>
The code above generates the following result.
Example 2
Calculate the similarity between two strings in percent:
<?php
similar_text("Hello World","Hello from java2s.com",$percent);
echo $percent;
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP soundex() Function
- Syntax for PHP soundex() Function
- Parameter for PHP soundex() Function
- Note for PHP soundex() Function
- Return for PHP soundex() Function
- Example - Calculate the soundex key of "Hello"
- Example - Using the soundex() function on two similar sounding words
Home » PHP Tutorial » PHP String Functions