Xafke Posted March 13, 2010 Share Posted March 13, 2010 Hi guys. I wanna write a script that checks how similar a string is to any string in an array. Let's say this is my array: "Name1" => "ABCD" "Name2" => "ABCDE" And let's say my string is ABC. The script should check how similar ABC is to any string in the array. So my string matched Name1 for 85%. And it matches name2 for 75%. The script should output the name (in this case name1 or name2) and should output howmuch it matched. I've tried with similar_text() but it can't deal with arrays. Thanks! Link to comment https://forums.phpfreaks.com/topic/195101-match-string-with-array/ Share on other sites More sharing options...
jskywalker Posted March 13, 2010 Share Posted March 13, 2010 can you descibe how this 85% or 75% is calculated? "ABC" has 3 characters, "ABCD" has 4 characters, so i would say they match for 3/4, of 75% "ABC" has 3 characters, "ABCDE" has 5 characters, so i would say they match for 3/5, of 60% Anw what about if you try to match "ABC" to "XYZABCDE" ? Link to comment https://forums.phpfreaks.com/topic/195101-match-string-with-array/#findComment-1025565 Share on other sites More sharing options...
Xafke Posted March 13, 2010 Author Share Posted March 13, 2010 If you match ABC to XYZABCDE you'll get a match of 54% This is calculated using PHP's similar_text(). But as sad before, it doesn't work with arrays. (http://php.net/manual/en/function.similar-text.php) Link to comment https://forums.phpfreaks.com/topic/195101-match-string-with-array/#findComment-1025576 Share on other sites More sharing options...
jskywalker Posted March 13, 2010 Share Posted March 13, 2010 <?php $text[1]="ABCD"; $text[2]="ABCDE"; $compare="ABC"; foreach ($text as $x) { $p=""; similar_text($compare, $x, &$p); $p=round($p,3); echo "$x compares to $compare: $p <br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/195101-match-string-with-array/#findComment-1025577 Share on other sites More sharing options...
Xafke Posted March 13, 2010 Author Share Posted March 13, 2010 This does everything that I want. But now there's one more thing I want. Your script show the result of everything. But I want that the script only show the biggest value. So it only shows the ones with the highest procent ($p in this case) Thanks! Link to comment https://forums.phpfreaks.com/topic/195101-match-string-with-array/#findComment-1025584 Share on other sites More sharing options...
jskywalker Posted March 13, 2010 Share Posted March 13, 2010 <?php $text[1]="ABCD"; $text[2]="ABCDE"; $compare="ABC"; $m=0; foreach ($text as $x) { $p=""; similar_text($compare, $x, &$p); $p=round($p,3); if ($p>$m) { $result="$x compares to $compare: $p <br>"; $m=$p; } echo "$x compares to $compare: $p <br>"; } echo "Max is:"; echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/195101-match-string-with-array/#findComment-1025591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.