note 15586 deleted from function.soundex by jimw

From: Date: Sat, 08 Dec 2001 23:36:03 +0000
Subject: note 15586 deleted from function.soundex by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-22348@lists.php.net to get a copy of this message
To the first contributer above, take a look at this function: /* Soundex2(): Differs from soundex() by converting the string to four digits without a leading letter. Enables phonetic matching of the strings first letter too. The algorithm goes like this: - Drop all occurrences of A, E, H, I, O, U, W and Y in the string. - Substitute each of the remaining letters in the string to one of the digits: (1: B, F, P, V)(2: C, G, J, K, Q, S, X, Z)(3: D, T)(4: L)(5: M, N)(6: R). - If consecutive letters of the original string substitutes to the same digit, delete all but the first digit. - Return the four first digits, possibly right padded with zeros, if less than four digits are produced. */ function soundex2($str) { $len=strlen($str)-1; for ($i=$j=$last=0; $j<=3 && $i<=$len; $i++) { switch ($str[$i]) { case "B": case "b": case "F": case "f": case "P": case "p": case "V": case "v": if (!$last || $last!="1") {$num.=$last="1"; $j++;} break; case "C": case "c": case "G": case "g": case "J": case "j": case "K": case "k": case "Q": case "q": case "S": case "s": case "X": case "x": case "Z": case "z": if (!$last || $last!="2") {$num.=$last="2"; $j++;} break; case "D": case "d": case "T": case "t": if (!$last || $last!="3") {$num.=$last="3"; $j++;} break; case "L": case "l": if (!$last || $last!="4") {$num.=$last="4"; $j++;} break; case "M": case "m": case "N": case "n": if (!$last || $last!="5") {$num.=$last="5"; $j++;} break; case "R": case "r": if (!$last || $last!="6") {$num.=$last="6"; $j++;} break; default: $last="0"; } } for (; 4-$j; $j++) $num.="0"; return $num; } Examples: soundex2("Clansy") == soundex2("Klansy") == '2452';

« previous php.notes (#22348) next »