Posted By

Cory on 07/15/08


Tagged

php search base fuzzy approximate


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jonhenshaw
brent-man
romanos


Approximate / Fuzzy search base.


 / Published in: PHP
 

This is a simple base for an approximate search application. You can currently use this to do search's but it, is not very sophisticated, right now anything that is as difference in characters base on the threshold will show up in the result, so with a threshold of 1 "the" would return, "he", "she", "thee", etc. But this is more made for a base for extending with your own capabilities.

  1. <?php
  2. class FuzzySearch {
  3. public $string;
  4. public $search;
  5.  
  6. public function __construct($string = null) {
  7. if(!empty($string)) {
  8. self::SetString($string);
  9. }
  10. }
  11.  
  12. public function SetString($string) {
  13. if(is_string($string)) {
  14. $this->string = $string;
  15. }
  16. }
  17.  
  18. public function SetSearch($search) {
  19. if(is_string($search)) {
  20. $this->search = $search;
  21. }
  22. }
  23.  
  24. public function PreformSearch($search, $string = null, $threshold = 1, $caseSensitive = false) {
  25. if(empty($search) && !empty($this->search)) { $search = $this->search; }
  26. if(empty($string) && !empty($this->string)) { $string = $this->string; }
  27. $matches = array();
  28.  
  29. if(isset($string) && strlen($string) > 0) {
  30. $words = explode(" ", $string);
  31.  
  32. foreach($words as $word) {
  33. $stripedWord = self::StripFormatting($word);
  34.  
  35. if(self::CompareStrings($search, $stripedWord, $caseSensitive) <= $threshold) {
  36. $matches[] = $stripedWord;
  37. }
  38. }
  39. }
  40. return $matches;
  41. }
  42.  
  43. public function CompareStrings($str1, $str2, $caseInsensitive = false) {
  44. $threshold = 0;
  45.  
  46. if($caseInsensitive) {
  47. $str1 = strtolower($str1);
  48. $str2 = strtolower($str2);
  49. }
  50.  
  51. if(strlen($str1) != strlen($str2)) {
  52. if(strlen($str1) > strlen($str2)) {
  53. $threshold = strlen($str1) - strlen($str2);
  54. }
  55. else if(strlen($str2) > strlen($str1)) {
  56. $threshold = strlen($str2) - strlen($str1);
  57. }
  58. }
  59.  
  60. for($i = 0; $i < strlen($str1); $i++) {
  61. if($i <= strlen($str2) - 1) {
  62. if($str1{$i} != $str2{$i}) {
  63. $threshold++;
  64. }
  65. }
  66. }
  67. return $threshold;
  68. }
  69.  
  70. public function StripFormatting($str) {
  71. $formatting = array( ".", ",", ";", ":", "|", "[", "]", "{", "}", "(", ")", "=", "+",
  72. "!", "@", "#", "$", "%", "^", "&", "*", "/", "\\", "<", ">", "?");
  73.  
  74. $str = str_replace($formatting, "", $str);
  75. $str = trim($str);
  76. return $str;
  77. }
  78. }
  79. ?>

Report this snippet  

You need to login to post a comment.