Monday, 2 February 2015

Percentage match strings with PHP

Percentage match strings with PHP

We have all seen, rather tediously, the matches on sites giving a percentage ranking of releavence to a search. Mostly these come from FULLTEXT searches from databases, but what if we simply want a percentage comparison between two strings? Here we show how simple this is.

<?php
/*** Show how broken it is ***/error_reporting(E_ALL);
/*** our first text string ***/$text_a "the quick brown fox jumps over the lazy dog";
/*** As string to compare it with ***/$text_b "The slow red fox jumps over the lazy cat";
/* check the similarities and put the percentage of 
 * matches into a variable.
 */
similar_text($text_a$text_b$percentMatch); 
/*** round off the match and echo ***/echo round($percentMatch); 
  
?>

0 comments:

Post a Comment