PHP similar_text()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Calculate the similarity of two strings and return the number of matching characters:

<?php
echo similar_text("Hello World","Hello Peter");
?>

Run Example »

Definition and Usage

The similar_text() function calculates the similarity between two strings.

This function can also calculate the percentage similarity between two strings.

Note:levenshtein()The function is faster than the similar_text() function. However, the similar_text() function provides a more accurate result with fewer required modifications.


Syntax

similar_text(string1,string2,percent)

Parameters Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to compare.
percent Optional. Specifies a variable name for storing the percentage similarity.

Technical Details

Return value: Returns the number of matching characters in the two strings.
PHP Version: 4+


More Examples

Example 1

Calculate the percentage similarity between two strings:

<?php
similar_text("Hello World","Hello Peter",$percent);
echo $percent;
?>

Run Example »


PHP String 参考手册PHP String Reference Manual Other Extensions