PHP Echo Tutorials

Using Similar Text

Written by phpecho.com   

In this short 'n' sweet tutorial, we're going to learn all about PHP's function called similar_text().

With similar_text(), you can compare two strings, and even get an output in percent format of how similar they are. This is a great addition for search functions, because it allows you to remove irrelevant results.

First of all, let's take a look at the anatomy of the function.

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

The third argument of the function is not necessary, but we will be using it today anyways.

Let's begin by creating two strings.

<?php
     $first_string = 'Photoshop';
 $second_string = 'Photography';

Now we'll plop in the function, and define $percent with the variable $p.

 similar_text($first_string, $second_string, $p);

Now to return the percent, and finish off the document.

 echo 'The strings ' . $first_string .  ' and ' . $second_string .  ' are ' . $p . '% similar!';
   ?>

And that's all in this tutorial from phpecho.com! Thanks for reading!