phpstringmatch

Simple PHP string matching pattern


I have two strings I need to compare. Let's say for example that the strings are as follows:

$A = "A fox jumped over the log";
$B = "A fox jumped";

Now how would I compare these two strings in PHP in order to return a percentage of character matching.

So I would like my algorithm to return (for the example above):

10/20 or .5 since $B has exactly half of the matches of the length of $A.

I figured I could iterate through each character, but this seemed a bit tedious and my research indicated that preg_match wouldn't help me much here.


Solution

  • Try the levenshtein or similar_text functions.