/ 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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class FuzzySearch { public $string; public $search; public function __construct($string = null) { self::SetString($string); } } public function SetString($string) { $this->string = $string; } } public function SetSearch($search) { $this->search = $search; } } public function PreformSearch($search, $string = null, $threshold = 1, $caseSensitive = false) { foreach($words as $word) { $stripedWord = self::StripFormatting($word); if(self::CompareStrings($search, $stripedWord, $caseSensitive) <= $threshold) { $matches[] = $stripedWord; } } } return $matches; } public function CompareStrings($str1, $str2, $caseInsensitive = false) { $threshold = 0; if($caseInsensitive) { } } } } if($str1{$i} != $str2{$i}) { $threshold++; } } } return $threshold; } public function StripFormatting($str) { "!", "@", "#", "$", "%", "^", "&", "*", "/", "\\", "<", ">", "?"); return $str; } } ?>