The Little Guy Posted July 7, 2010 Share Posted July 7, 2010 I would like to search a database for users that are similar to the current user. I don't know If this can be done with MySQL, or PHP. So, for example, say I have the following user: billy123 Some similar users could be as follow: billy billy1 billy2010 bill billybobjoe What is a way this can be done, because I am stumped here. Thanks! Link to comment https://forums.phpfreaks.com/topic/207041-related-items/ Share on other sites More sharing options...
gwolgamott Posted July 7, 2010 Share Posted July 7, 2010 Investigate the use of levenshtein() & similar_text(). A combination of those two may get what you want. I used this simple search to go through folders and give similar and possible matches... very rudimentary but works super fast and returned matches deemed good for what I was using it on as a search of documents in a folder library. <?php //this function returns if one of the strings is actually with the other string // returns yes if same or one string is found within the second. function Weigh_Results($One, $Two) { if ( stristr( $One, $Two ) ) { $ser_results = "yes"; } else { $ser_results = "no"; } return $ser_results; } ?> //percentage tests of two combined that give similiar phonotic and similiar spelling adjust this to your liking //returns yes or no as to whether the it's close enough to be a return <?php function Meta_Lev($One , $Two) { $str1 = $One; $str2 = $Two; $meta_one=metaphone($str1); $meta_two=metaphone($str2); $lev = levenshtein($meta_one, $meta_two); //echo "metaphone code for ".$str1." is ". $meta_one; //echo "<br />"; //echo "metaphone code for ".$str2." is ". $meta_two."<br>"; //echo "<br />"; //echo "levenshtein of the two metaphones is:".$lev.".<br>"; $length = strlen($str1); $match = $lev / $length; //echo "percentage of length is: ".$match.".<br>"; if($match < .2) { $ret = "yes"; } if($match >= .2) { //echo "metaphone codes are not matching<br>"; similar_text($str1,$str2,$percent); //echo "Percentage of similar texts are: ".$percent.".<br>"; if($percent > 40) { $ret = "yes"; } else { $ret = "no"; } } return $ret; } ?> Link to comment https://forums.phpfreaks.com/topic/207041-related-items/#findComment-1082636 Share on other sites More sharing options...
kenrbnsn Posted July 7, 2010 Share Posted July 7, 2010 Use the "like" condition in the "where" clause in your mysql query: <?php $current_user = "billy"; $q = "select username from tablename where username like '%$current_user'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/207041-related-items/#findComment-1082651 Share on other sites More sharing options...
The Little Guy Posted July 7, 2010 Author Share Posted July 7, 2010 What about this? <?php $current_user = "billy"; $q = "SELECT username FROM tablename WHERE username SOUNDS LIIKE '$current_user'; ?> Does that return good relevant results? Link to comment https://forums.phpfreaks.com/topic/207041-related-items/#findComment-1082672 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.