1

I want to find number of similar words between two texts

Example

$str1=the cat is on the roof  
$str2=the mouse is on the roof

the,is,on,the,roof words are similar in $str1 and $str2

So output will be in number 5 OR In percentage 86%

I am try similar_text() function but this function not work as which i want.

5
  • 4
    Hey, the code ran away, go and get it, hurry! Jan 16, 2014 at 14:22
  • How did you get 86%? There are 12 words total and 10 are matches and that gives 83% - where does the extra 3% come from? Jan 16, 2014 at 14:27
  • And similar_text provides 83% as well, sure looks right to me
    – jszobody
    Jan 16, 2014 at 14:28
  • @jszobody If I am change word sequence thn output comes different try this string............ str1="the cat is on the roof" $str2= "the on the roof mouse is"
    – Me7888
    Jan 16, 2014 at 14:49
  • I suggest string tokenizer. php.net/strtok
    – Saif Hamed
    Jan 16, 2014 at 15:00

4 Answers 4

7

Easy, explode them and then use array_diff:

$totalWords = count($array_1);

$array_1 = explode(" ", $str1);
$array_2 = explode(" ", $str2);
$differenceCount = count(array_diff($array_1, $array_2));

$differentPercent = $differenceCount / ($totalWords / 100);

@Edit:

Edited code above to display percentage. However remember you may have a wrong result if the word count of array 1 and array 2 is not identical.

6
  • thnx @Steini for giving answer ...can possible to check how much percentage of words similar between two string...
    – Me7888
    Jan 16, 2014 at 14:35
  • I am try edited code....display operand error : <b> error Unsupported operand types</b>
    – Me7888
    Jan 16, 2014 at 14:46
  • I forgot a ; at the end of the code, updated it however you should know a little about coding / errorhandling before asking such questions. This site is to give solution hints, not completely working systems.
    – Steini
    Jan 16, 2014 at 14:53
  • thnx steini ...it working now ...but wrong result display.....try this two string $str1 = 'make higher the USA pie'; $str2 = 'make higher pie the USA'; Output : 28.571428571429 %
    – Me7888
    Jan 16, 2014 at 15:06
  • steini PLZ update your answer with change 2nd last line : $differenceCount = count(array_diff($arr1, $arr2));
    – Me7888
    Jan 16, 2014 at 15:22
3
$arr1 = explode(" ",$str1)
$arr2 = explode(" ",$str2)

$arr3 = array_diff($arr1, $arr2);

echo(count($arr1)-count($arr3));
2

The way I would approach this is to explode each string and then use array_diff to compare them like this:

$arr1 = explode(' ', $str1);
$arr2 = explode(' ', $str2);
$diff = array_diff($arr1, $arr2);
echo (count($arr1) - count($diff));

That will echo out the number of similar words.

4
  • There are 2 almost identical answers already, was that really nesessary?
    – Steini
    Jan 16, 2014 at 14:33
  • 1
    @steini They weren't there when I typed my answer, look at the times, really close. There were no answers when I started.
    – Styphon
    Jan 16, 2014 at 14:33
  • Fine I remove it but still you can click the refresh button before sending the post There are 3 minutes between existing posts and yours...
    – Steini
    Jan 16, 2014 at 14:33
  • Hi Steini and @Styphon ...........can possible to check how much percentage of words similar between two string
    – Me7888
    Jan 16, 2014 at 14:41
0
$arr1 = explode(" ",$str1)
$arr2 = explode(" ",$str2)

$arr3 = array_diff($arr1, $arr2);

i used array_intesect to check how many matches,, i used this in searching one array into other

and for preventing with auxilary verbs and prepositions(the,to,a,are etc) use -

$arr1 = str_replace(array("to", "the","a","an","in","by","but","are","is","had","have","has"),'',$arr1); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.