Finding how much of one string matches another
Moderator: General Moderators
Finding how much of one string matches another
I was wondering if it is possible if I have a sentence or subject and was to check it with another subject if there is a way to get it to display if say 90% of it matches. Meaning if all but the last words was the same it would match.
Pimptastic | Please use
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="Extremest"]ok so then pretty much like do a count of the array to see how many words and then add up how many it finds and do a divide to get a percent...if above this amount then it is a match.....Am I getting that right?[/quote]
thats the best way...Code: Select all
$string1 = "hello1 hello2 hello3 hello4";
$string2 = "hello1 hello2 hello3";
$arr = explode($string2, " ");
$numstring2 = count($arr);
$numofmatch = 0;
for($i=0;$i<$numstring2;$i++)
{
if(strpos($string1, $arr[$i]) !== false)
$numofmatch++;
}
$ratio = ($numstring2/$numofmatch)*100;Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]Pimptastic | Please use
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
explode works with any characterCode: Select all
explode(<string your exploding>, <character that separates the elements)
so for exemple
$string1 = "gggaggaggag";
$arr = explode($string1, "a");
would give you:
$arr[0] = "ggg";
$arr[1] = "gg";
$arr[2] = "gg";
$arr[3] = "g";Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]Pimptastic | Please use
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ok here's what you do:
So you first have a set of words someone entere right, lets say they are in this variable:Code: Select all
$search ="string containing a set of words";
$arr = explode($search, " ");
$numofwords = count($arr);
$numofmatches = 0;
$result = mysql_query("SELET * FROM <tablename>");
$num = mysql_num_rows($result); //get the number of rows
for($i=0;$i<$num;$i++) //loop thru them
{
$row = mysql_fetch_object($result); //grab the rows
for($j=0;$j<$numofwords;$j++)
{
if(strpos($row->post, $arr[$i]) !== false)
$numofmatch++;
}
//right here you just searched the current post, $numofmatch contains the number of words that the post and the search string have in common
$numofmatches = 0; //set your counter back to 0, then loop again thru the next post.
}Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Flamie, please use the syntax highlighting tags.
Extremest, you could just use similar_text() instead.
Extremest, you could just use similar_text() instead.