Using PHP similar_text() Function to Compare String Similarity

This article explains the PHP similar_text() function, its syntax, how to use it with code examples to calculate the similarity percentage between two strings, and important considerations such as case sensitivity, length limits, and result accuracy.

php Courses
php Courses
php Courses
Using PHP similar_text() Function to Compare String Similarity

The similar_text() function in PHP compares two strings and returns an integer representing their similarity percentage.

Syntax:

int similar_text ( string $first , string $second [, float &$percent ] )

The parameters $first and $second are the strings to compare, while the optional $percent variable receives the similarity percentage.

Usage example: To compare "Hello World" with "Hello PHP", you can use the following code:

$first = "Hello World";<br/>$second = "Hello PHP";<br/><br/>similar_text($first, $second, $percent);<br/><br/>echo "Similarity: " . $percent . "%";

This code passes the two strings to similar_text(), stores the percentage in $percent, and outputs the result.

Important notes:

The function is case‑sensitive, so uppercase and lowercase characters are treated as different.

Very long strings may cause high execution time or exceed PHP's time limits.

The similarity percentage is algorithm‑based and may not be perfectly accurate; results should be interpreted accordingly.

Summary: similar_text() provides a convenient way to measure string similarity in PHP, returning an integer percentage, but developers should be aware of case sensitivity, length constraints, and the inherent limitations of the algorithm.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentPHPstring similaritysimilar_text
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.