Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

similar_text

Calculates how similar two strings are.

Syntax

similar_text(string $string1, string $string2, float &$percent = null): int

Parameters

ParameterTypeDescription
$string1stringThe first string.
$string2stringThe second string.
$percentfloatOptional. Receives the similarity as a percentage.

Return value

intThe number of matching characters.

Examples

Similarity as a percentage:

<?php
 
similar_text('World', 'Word', $percent);
 
echo round($percent, 1), '%';

Output:

88.9%

Notes

The function is slow on long strings. For spotting typos levenshtein() is used more often.

See also

  • levenshtein — Counts how many edits turn one string into another.
  • strcmp — Compares two strings.
  • soundex — Returns the sound code of an English word.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.