Compare Two Arrays for Matching Phrases

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gprince66
Forum Newbie
Posts: 6
Joined: Tue Feb 14, 2012 11:55 pm

Compare Two Arrays for Matching Phrases

Post by gprince66 »

Hi,

I would like to compare two arrays for matching phrases. I have searched everywhere for a solution. Any suggestions would be greatly appreciated.

The php code below produces this array...

Array ( [0] => This [1] => tool [3] => compare [4] => two [5] => articles [7] => show [8] => any [9] => duplicate [10] => content
[11] => of [14] => more [15] => word [19] => two [20] => word [21] => phrases [22] => should [23] => be [24] => ignored. )

How do I produce the following array by ignoring 1 & 2 word phrases and only counting 3 or more consecutive matching word phrases?

Array ( [0] => compare two articles [1] => show any duplicate content of [2] => two word phrases should be ignored )

Thanks in advance,

Gary

Code: Select all

// get user input
$str1 = 'This tool will compare two articles and show any duplicate content of 3 or more word phrases. One and two word phrases should be ignored.';
$str2 = 'This tool compare two articles show any duplicate content of more word. two word phrases should be ignored.';

// explode strings into seperate words
$str1array = explode(" ", $str1);
$str2array = explode(" ", $str2);

// compare duplicate words in the two arrays
$dupwords = array_intersect($str1array, $str2array);
print_r($dupwords);
Hitman47
Forum Newbie
Posts: 6
Joined: Thu Jan 26, 2012 5:58 pm

Re: Compare Two Arrays for Matching Phrases

Post by Hitman47 »

You would have to create your own scan algorithm and once a match is found continue onto the next word in each array to find out if there are consecutive matching words. This is where some math and basic computer logic come in handy. There are many ways to scan and search in arrays - each with its own benefits and consequences.

The most basic way would be to choose one of the arrays, start at index 0. Then compare Array1[0] to all off the values in Array2. If no match is found, move onto Array1[1]. If a match is found with let's say, Array1[1]==Array2[5], then compare the next words. If Array1[2]==Array2[6] then you now know you have 2 consecutive words. You can keep going until it fails, then return the results.
gprince66
Forum Newbie
Posts: 6
Joined: Tue Feb 14, 2012 11:55 pm

Re: Compare Two Arrays for Matching Phrases

Post by gprince66 »

Thanks for the fast reply Hitman47,

I am new to php and somewhat understand what you are suggesting, but have no idea how to code this type of array scan algorithm. Do you know of any websites that have any code snippets I could analyze for solving this problem?
Post Reply