function soundex ($word
{
$letters = 'abcdefgijklmnopqrstuvxyz'
$codes = '012301202245501262301202'
$word = strtolower$word);
$soundex = ''
$count = 0;
for ($ii=0; $iistrlen$word); $ii++) {
$letter = $word$ii];
$p = strpos$letters, $letter);
if ($p!==false) {
if ($codes$p]!==substr$soundex,-1)) {
$soundex .= $codes$p];
if ($codes$p]!='0') $count++; //count only consonants
if ($count>=5) break; //if we have 5 characters we're sure we can break
}
}
}
// add initial letter
$init = substr$word, 0, 1);
$p = strpos$letters, $init);
if ($p===false)
$soundex = strtoupper$init).$soundex
else
$soundex = strtoupper$init).substr$soundex, 1);
// get rid of the vowels
$soundex = str_replace'0', '', $soundex);
// trim or pad
if (strlen$soundex)>=4) {
$soundex = substr$soundex, 0, 4);
} else {
$soundex .= str_repeat'0', 4-strlen$soundex));
}
return $soundex
}