SoundEx
Source code for SoundEx Script
<?php
/*****************************
Created from information found on wikipedia,
Author: Jesse A. Teates (http://www.bawk.org/)
Feel free to modify, improve and use this code in any way you like,
I only ask that you leave this bit in your file, and give credit where due.
Appreciated, and enjoy!
*****************************/
class SoundEx {
protected $letter_Translation = array('b'=> 1, 'f'=>1, 'p'=>1, 'v'=>1,
'c'=>2, 'g'=>2, 'j'=>2, 'k'=>2,
'q'=>2, 's'=>2, 'x'=>2, 'z'=>2,
'd'=>3, 't'=>3, 'l'=>4, 'm'=>5,
'n'=>5, 'r'=>6);
protected $letter_Removal = array('a','e','h','i','o','u','w','y');
function Encode($String)
{
$String = strtolower($String);
$prefix = substr($String, 0, 1);
$String = substr($String, 1);
/**
** Remove each letter contained in the $letter_Removal array.
*/
foreach ($this->letter_Removal as $letter) {
$String = str_replace($letter, '', $String);
}
/**
** Iterate through each letter of our string, and replace each letter with the corresponding keys value from the $letter_Translation array
*/
for ($i = 0; $i <= strlen($String); $i++) {
$cur_Letter = $String[$i];
if (array_key_exists($cur_Letter, $this->letter_Translation)) {
$let_Pos = strpos($String, $cur_Letter);
$String = substr_replace($String, $this->letter_Translation["$cur_Letter"], $let_Pos, 1);
}
}
/**
** Check for same-number combination sequences and remove all but the first.
*/
for ($i = 0; $i <= strlen($String); $i++) {
$cur_Num = $String[$i];
if ($String[$i] == $String[($i + 1)]) {
$newstr = substr($String, 0, $i + 1);
$newstr2 = substr($String, $i + 1);
$newstr2 = str_replace($cur_Num, '', $newstr2);
$String = $newstr . $newstr2;
}
}
/**
** If the string is less than 3 digits, append 0's until it reaches a length of 3 digits.
*/
while (strlen($String) < 3) {
$String .= '0';
}
/**
** Return the prefix, follow by the translated 3 digit code.
*/
return strtoupper($prefix . substr($String,0,3));
}
}
?>