PDA

View Full Version : Matching Words


Tien
12-25-05, 01:25 AM
Hi,

I'm trying to make the script to match words from two strings

for instance,
$string1 = "Tom, bob, sarah, cat, dogs";
$string2= "birds, candles, rat, cat";

if you notice that both strings contains the word 'cat'

Now, how do I ask the script to look up that there is a match between the two strings and echo out "We have cat"

Basically it's like one of those feature that livejournal or deadjournal where it matches interest from users.

kurniawan
12-25-05, 07:06 AM
You can use combination of explode and stristr, so the code will be :
$string1 = "Tom, bob, sarah, cat, dogs";
$str1_array=explode(", ",$string1); // explode the string
$str_count=count($str1_array);
$string2= "birds, candles, rat, cat";
FOR($i=0;$i<$str_count;$i++){
IF(stristr($string2,$str1_array[$i]))
{$condition="OK";}
}
IF($condition=="OK")
{ echo "We have cat"; }

source : http://jp2.php.net/manual/en/function.stristr.php

Anatoliy
12-25-05, 08:44 AM
Kurniawan, you better don't post anything than a script that doesn't work.
1. It should be {$condition="OK";} not {condition="OK";}.
2. Script doesn't work even after fixing this error.
3. Even if script would work it has an error in its logic. If there will be a match word "ret" in both strings your script will say "We have cat".

Anatoliy
12-25-05, 09:08 AM
This one works.

<?
$string1 = "Tom, bob, sarah, cat, dogs";
$string2= "birds, candles, rat, cat";
$array1 = explode(", ", $string1);
$array2= explode(", ", $string2);
foreach($array1 as $tmp){if(in_array($tmp, $array2)) echo "We have $tmp.<br>";}
?>

kurniawan
12-25-05, 09:29 AM
Anatoly,
Thank you for the correction :D I fixed it and it works. I am curious about your statement
3. Even if script would work it has an error in its logic. If there will be a match word "ret" in both strings your script will say "We have cat".
Can you explain it to me? I am still learning :)

Anyway the first posting said :
Now, how do I ask the script to look up that there is a match between the two strings and echo out "We have cat"

So, I thought Tien only wanted to find any match, he didn't care how many matching he can find. By the script you gave, he will have two lines "We have $tmp". I admit your script is much better :dd I just tried to give him an understanding from the base.

Thanks again.

Anatoliy
12-25-05, 09:57 AM
Anatoly,
Thank you for the correction :D
You are welocome.
I fixed it and it works.
Good job!


I am curious about your statement
3. Even if script would work it has an error in its logic. If there will be a match word "ret" in both strings your script will say "We have cat".
Can you explain it to me? I am still learning :)

echo "We have cat";

By the script you gave, he will have two lines "We have $tmp".

Nope.

I just tried to give him an understanding from the base.

But actually you made a confused guy even more confused.

Thanks again.

You are welcome again. ;)

kurniawan
12-25-05, 10:04 AM
By the script you gave, he will have two lines "We have $tmp". ==> I meant, if the two string have two same values then he will have 2 "We have $tmp"s. :D
Next time I'll be careful.

Anatoliy
12-25-05, 10:42 AM
You mean if $string2 will have two same values? In this case you are right. He will get "We have $tmp" twice. He didn't tell anything about such possibility. Let him read our posts and if he will tell us that it can accure we can add few lines of code to avoid the prob.

Tien
12-25-05, 01:01 PM
Guys, thanks for responding.

The purpose of this script is for my user section where they have a field 'animal' where they can enter their favorite animals separate by comma.

And when the script is running, it will search through all members and look in the field 'animal' and if there is a match on a specific animal, it will turn into a link where everyone can click and see which members share that specific animal. This is complicate for me to explain. That's why I brought up the ideas of 'interest' for deadjournal and livejournal.

So this is how it suppose to be working. When we view this user's profile, it will have a section "My animal" where it listed all their animal separate by a comma. And along those listed animal, it will have a link on any animal that matches with other member's animal. So that users can click on that link and find other users that share same animal.

Sorry guy, if my explaination is not clear enough

HalfaBee
12-25-05, 08:16 PM
You need something like this.

<?
$string1 = 'dog , cat ,rooster, pig';
$string2 = 'dog,cat';

$array1 = explode( ',' , str_replace( ' ','' , $string1 ) );
$array2 = explode( ',' , str_replace( ' ','' , $string2 ) );

$matches = array_intersect( $array1 , $array2 );
if( count($matches>0) ) {
foreach( $matches as $animal ) {
$links[] = "<A href=page.php?a=$animal>$animal</a>";
}
}
echo str_replace( $matches , $links , $string1 );

?>

Tien
12-25-05, 08:57 PM
HalfaBee, That's what I'm looking for.

Here's the thing. I have like 100 members. Say that $string1 belongs to current viewing member, how do I match it with 99 other members?

HalfaBee
12-26-05, 12:27 AM
Are you using a mysql DB for the members?
If you are I would just make a link (as above) for each animal the user has and do a query for each animal page to list the users using a query like

SELECT * from members_table where animals LIKE '%$a%'

You might get a few false matches with animals that have similar names eg cat will match catipillar, you could filter these out when fetching the query.

Tien
12-26-05, 01:28 AM
Not a bad Idea