note 16907 added to function.soundex

From: Date: Sun, 18 Nov 2001 16:23:26 +0000
Subject: note 16907 added to function.soundex
Groups: php.notes 
Request: Send a blank email to php-notes+get-20980@lists.php.net to get a copy of this message
Here is my working code for SOUNDEX with mySQL. One of the most important functions for phonetic searches (error tolerant queries): function soundex2($str){ $str=strtolower($str); $len=strlen($str); for($i=$j=$last=0;$j<=3&&$i<=$len;$i++){ switch($str[$i]){ case"b":case"f":case"p":case"v": if(!$last||$last!="1"){$num.=$last="1";$j++;} break; case"c":case"g":case"j":case"k":case"q":case"s":case"x":case"z": if(!$last||$last!="2"){$num.=$last="2";$j++;} break; case"d":case"t": if(!$last||$last!="3"){$num.=$last="3";$j++;} break; case"l": if(!$last||$last!="4"){$num.=$last="4";$j++;} break; case"m":case"n": if(!$last||$last!="5"){$num.=$last="5";$j++;} break; case"r": if(!$last||$last!="6"){$num.=$last="6";$j++;} break; default:$last="0"; } } return $num; } function soundex1($str){ $WordArray=explode(" ",$str); $SoundexCodec=""; $Word=""; for($i=0;$i<count($WordArray);$i++)$Word.=$WordArray[$i]; for($i=0;$i<strlen($Word);$i=$i+4) { $SoundexCodeTMP=soundex2(substr($Word,$i,4)); $SoundexCodec.=$SoundexCodeTMP; } return $SoundexCodec } //Example $database="YOUR_DB"; $table="YOUR_TABLE"; $row="YOUR_TABLE_ROW"; $MyText="example soundex code"; $SoundexCode=soundex1($MyText); $result=@mysql_db_query($database,"SELECT from $table WHERE SOUNDEX($row) LIKE '%$SoundexCode%'"; If you echo $SoundexCode from $MyText, you receive 251425323 For shorter words you receive 1 or 2 numbers more at the beginning, but this is equal. -- http://www.php.net/manual/en/function.soundex.php http://master.php.net/manage/user-notes.php?action=edit+16907 http://master.php.net/manage/user-notes.php?action=delete+16907 http://master.php.net/manage/user-notes.php?action=reject+16907

« previous php.notes (#20980) next »