PHP similar_text() Function
Topic: PHP String ReferencePrev|Next
Description
The similar_text()
function calculates the similarity between two strings.
The following table summarizes the technical details of this function.
Return Value: | Returns the number of matching characters in both strings. |
---|---|
Version: | PHP 4+ |
Syntax
The basic syntax of the similar_text()
function is given with:
similar_text(string1, string2, percent);
The following example shows the similar_text()
function in action.
Example
Run this code »<?php
// Sample strings
$str1 = "Hello, John";
$str2 = "Hello, Alexander";
// Calculating similarity and print
echo similar_text($str1, $str2);
?>
Parameters
The similar_text()
function accepts the following parameters.
Parameter | Description |
---|---|
string1 | Required. Specifies the first string to be compared. |
string2 | Required. Specifies the second string to be compared. |
percent | Optional. Specifies a variable to store the similarity in percent. |
More Examples
Here're some more examples showing how similar_text()
function actually works:
The following example shows how to calculate the similarity between two strings in percent.
Example
Run this code »<?php
// Sample strings
$str1 = "Hello, John";
$str2 = "Hello, Alexander";
// Calculating similarity and print
similar_text($str1, $str2, $percent);
echo $percent;
?>