d3wannabe d3wannabe - 6 months ago 8
PHP Question

PHP case insensitive count of words common between 2 strings

I'm trying to take 2 pieces of text in php like this...

"A cat jumped over the hat"

"The mad hatter jumped over his cat"


And get results like this...

the
cat
jumped
over


(i.e. the common words between the strings, where hat is NOT included because it's part of another word in the second string)

I've found a bunch of examples to help count occurrences of 1 string within another, but that would end up giving me the "hatter" problem so I'm guessing I need to tokenize both strings into word-lists and do one-to-one compares somehow.

Struggling to visualise an efficient way to achieve that though so appreciate any thoughts at all on what the correct approach is. Thanks!

Answer

For this problem, I'd use explode to separate each string into words, then create an array for each string where the keys are the words, and the values all just true. Then, you can take one of the arrays, loop through its keys, and check whether they're present in the other array.