08-09-2009, 06:36 PM
|
#1 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
SoundEX Algorithm: Knuth
Well I needed to employ the soundex algorithm for some simple search techniques in a script I'm writing and without doing research I started writing my own class to calculate the soundex...and than found the soundex function when I typed out SoundEX into my IDE...So I figured I'd atleast post it up here to share with people who'd be interested.
php Code:
<?phpclass SoundEX{ private static function encode ( $String ) { switch( strtolower($String) ) { case 'b': case 'f': case 'p': case 'v': return "1"; case 'c': case 'g': case 'j': case 'k': case 'q': case 's': case 'x': case 'z': return "2"; case 'd': case 't': return "3"; case 'l': return "4"; case 'm': case 'n': return "5"; case 'r': return "6"; default: return null; } } //As described by Knuth public static function generateSoundEX ( $String ) { if( strlen($String) > 0 ) { $String = strtolower($String); $outPut .= $String{0}; for( $i = 1; $i < strlen($String) && strlen($outPut) < 4; $i++ ) { $c = self:: encode($String{$i}); switch( $String{$i - 1 } ) { case 'a': case 'e': case 'i': case 'o': case 'u': $outPut .= $c; break; case 'h': case 'w': default: if(strlen($outPut) == 1 ) { if(self:: encode($outPut[strlen ($outPut)- 1] ) != $c) $outPut .= $c; } else { if($outPut[strlen ($outPut)- 1] != $c) $outPut .= $c; } break; } } for( $i = strlen($outPut); $i < 4; ++ $i ) { $outPut .= "0"; } return (string )$outPut; } }}
|
|
|
|