Welcome to WebmasterWorld Guest from 54.227.48.147

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Match multiple words in any order

More PHP matching noobiness ;)

     
11:04 pm on Nov 28, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 6, 2003
posts:630
votes: 0


Is there a way for me to match occurances of a series of words in string, regardless of the order in which they occur?

For instance, if my string to find is 'make the pie higher', I want to be able to match this in text of 'higher the pie make' (I think I'm starting to sound like yoda here ;))

The only way my somewhat less than perfect PHP coding mind can think of to make this work is:

- count the number of words to find
- match them one by one
- check there were the same number of matches as the number of words

While this works, it strikes me as an ugly and inefficient way of doing things - does anyone have any better suggestions?

3:59 am on Nov 29, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 13, 2003
posts:775
votes: 0


Hello Yoda,

Here's a shot at it...

$myString1 = 'make the pie higher';
$myString2 = 'higher the pie make';

//Get the words into arrays
$myWords1 = explode(" ", $myString1);
$myWords2 = explode(" ", $myString2);

//Sort each array to match up words
sort($myWords1);
sort($myWords2);

//Compute differences in arrays with index checking
$result = array_diff_assoc($myWords1,$myWords2);

//If an array is returned then something is not matching
if(empty($result)) {
echo "match";
} else {
echo "fail";

I'm using array_diff_assoc to catch any double words like 'higher higher the pie make'. If you're not interested in this then just leave out the sort functions and use array_diff.

Tim

11:48 pm on Nov 29, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 6, 2003
posts:630
votes: 0


Thanks Tim - you did exactly what I asked for.

Unfortunately, I wasn't asking for the right thing ;)

I wanted to match the occurance of all the words of 'make the pie higher' in a string of many words. I've ended up with a variation of my original code:


$pastry="mmmm, the pie higher I want to make";
$pie="make the pie higher";
$count = count(explode(" ", $pie));
$piearray = explode(" ", trim($pie));
foreach ($piearray as $value) {
if (preg_match("/\b".$value."\b/im", $pastry))
{$found=$found+1;}
}
if ($found==$count ) {
// the pie is higher
}
else {
// no pie for me
}

I'm not sure if this is an efficient approach or not (any comments?).

4:33 am on Nov 30, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 3, 2004
posts:598
votes: 0


I think you make all right, but may be if you use a database, may be better use database functions to search such material there?
2:23 pm on Nov 30, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 13, 2003
posts:775
votes: 0


Uh, maybe I still don't understand ;-)

Anyway, is there any reason why you're using preg_match? This little tip is from the manual [php.net]

Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

Tim

5:27 pm on Nov 30, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 6, 2003
posts:630
votes: 0


any reason why you're using preg_match

Yeh - the code I posted is simplified, but the matching is actually quite complex.