I am currently trying to find a percentage of how many words in two strings match
the idea is that you match one string against another and get a % of how similar they are.
what i have at the moment is a rough idea and was wondering if i could get some help the idea is to turned each string into an array you could iterate through the list one by one and adding 1 to $matches if there is a match and 0 if there isn't
<?php>
$originalText = "The quick brown fox jumps over the lazy dog";
$comparisonText = "The quick red fox leaps over the sleeping dog";
//to get a count the number of words we are trying to match
$strNum = str_word_count($originalText);
$arroriginalText = explode(" ", $originalText);
$arrcomparisonText = explode(" ", $comparisonText);
//THIS IS WHERE I'M STUCK
//creating some form of a loop to go through the array of strings
if (preg_match("*word from $arroriginalText*, *word from $arrcomparisonText*", $matches)) {
//not fully understanding what to put here
}
//i'm bad at maths
$strNum - $matches = $percentageFind
$percentageFind / $strNum = $decimal
$decimal * 100 = $theAnswer
?>
I'm not sure if I've made what I'm thinking clear but any help would be much appreciated.
$matches = 0;
if( empty( $arroriginalText ) ) {
echo 'Empty';
die();
}
for( $n = 0; $n < count( $arroriginalText ); $n++ ) {
if( $arrcomparisonText[$n] === $arroriginalText[$n] ) {
$matches++;
}
}
$percentage = 100 * $matches / count( $arroriginalText );
or better yet (untested)
$percentage = count( array_diff( $arroriginalText, $arrcomparisonText ) ) / count( $arroriginalText );