similar_text
Calculates how similar two strings are.
Syntax
similar_text(string $string1, string $string2, float &$percent = null): intParameters
| Parameter | Type | Description |
|---|---|---|
| $string1 | string | The first string. |
| $string2 | string | The second string. |
| $percent | float | Optional. 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.