W3docs

similar_text()

Our article is about the PHP function similar_text(), which is used to calculate the similarity between two strings. This function is useful for comparing two

The PHP similar_text() function measures how alike two strings are. Instead of a plain yes/no comparison, it counts the number of matching characters and can also report that match as a percentage. This makes it handy for fuzzy matching tasks like detecting near-duplicate entries, suggesting "did you mean…?" corrections, or ranking search results by closeness.

This chapter covers the syntax, the return value, the optional percentage argument, the gotchas to watch for, and a worked example you can run.

Syntax

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

The function takes three parameters:

  • $string1 and $string2 — the two strings to compare.
  • $percent — optional. If you supply a variable here, it is passed by reference and set to the similarity percentage (a float from 0 to 100).

The return value is an integer: the number of matching characters between the two strings, found using the longest common substring algorithm applied recursively.

How the percentage is calculated

The percentage is (matches * 2) / (length1 + length2) * 100. So a value of 100 means the strings are identical, and 0 means they share nothing in common. Because the formula uses both lengths, the percentage is the same no matter which order you pass the strings in.

Basic example

php— editable, runs on the server

We initialize $percent to 0 first so PHP does not raise an "undefined variable" warning, then pass it by reference. The output is:

6
60

The two strings share 6 matching characters (Hello — the word "Hello" plus the space), which works out to a 60% similarity.

Things to watch out for

It is case-sensitive. 'Hello' and 'hello' are not treated as equal — the first character differs. Normalize with strtolower() first if case should be ignored:

<?php
$a = strtolower('Hello');
$b = strtolower('hello');
similar_text($a, $b, $percent);
echo $percent; // 100
?>

The percentage variable must already exist. Since $percent is passed by reference, declare it before the call (e.g. $percent = 0;) to avoid warnings.

It is order-insensitive for the percentage, but not free. similar_text() is more expensive than a simple comparison because of its recursive algorithm. For very long strings or large datasets, profile before relying on it in hot paths.

similar_text() vs. levenshtein()

Both functions measure string similarity, but they answer different questions:

  • similar_text() counts matching characters and gives a similarity score — higher means more alike.
  • levenshtein() counts the edits (insertions, deletions, substitutions) needed to turn one string into the other — lower means more alike.

Use similar_text() when you want a percentage of closeness; use levenshtein() when you care about how many keystrokes separate two strings, such as for spell-checking.

Conclusion

The similar_text() function is a practical tool for fuzzy string comparison. Return the integer count of matching characters, pass an optional reference variable to get the percentage, and remember it is case-sensitive. For related comparison tools, see strcmp() for exact comparison, levenshtein() for edit distance, and soundex() for phonetic matching.

Practice

Practice
What does the similar_text() function do in PHP?
What does the similar_text() function do in PHP?
Was this page helpful?