note 45843 added to function.soundex
| From: | justin at NO dot blukrew dot SPAM dot com | Date: | Tue, 21 Sep 2004 11:18:43 +0000 |
| Subject: | note 45843 added to function.soundex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-77099@lists.php.net to get a copy of this message | ||
I originally looked at soundex() because I wanted to compare how individual letters sounded. So,
when pronouncing a string of generated characters it would be easy to to distinguish them from
eachother. (ie, TGDE is hard to distinguish, whereas RFQA is easier to understand). The goal was to
generate IDs that could be easily understood with a high degree of accuracy over a radio of varying
quality. I quickly figured out that soundex and metaphone wouldn't do this (they work for
words), so I wrote the following to help out. The ID generation function iteratively calls
chrSoundAlike() to compare each new character with the preceeding characters. I'd be interested
in recieving any feedback on this. Thanks.
<?php
function chrSoundAlike($char1, $char2, $opts = FALSE) {
$char1 = strtoupper($char1);
$char2 = strtoupper($char2);
$opts = strtoupper($opts);
// Setup the sets of characters that sound alike.
// (Options: include numbers, include W, include both, or default is none of those.)
switch ($opts) {
case 'NUMBERS':
$sets = array(0 => array('A', 'J', 'K'),
1 => array('B', 'C', 'D', 'E',
'G', 'P', 'T', 'V', 'Z', '3'),
2 => array('F', 'S', 'X'),
3 => array('I', 'Y'),
4 => array('M', 'N'),
5 => array('Q', 'U', 'W'));
break;
case 'STRICT':
$sets = array(0 => array('A', 'J', 'K'),
1 => array('B', 'C', 'D', 'E',
'G', 'P', 'T', 'V', 'Z'),
2 => array('F', 'S', 'X'),
3 => array('I', 'Y'),
4 => array('M', 'N'),
5 => array('Q', 'U', 'W'));
break;
case 'BOTH':
$sets = array(0 => array('A', 'J', 'K'),
1 => array('B', 'C', 'D', 'E',
'G', 'P', 'T', 'V', 'Z', '3'),
2 => array('F', 'S', 'X'),
3 => array('I', 'Y'),
4 => array('M', 'N'),
5 => array('Q', 'U', 'W'));
break;
default:
$sets = array(0 => array('A', 'J', 'K'),
1 => array('B', 'C', 'D', 'E',
'G', 'P', 'T', 'V', 'Z'),
2 => array('F', 'S', 'X'),
3 => array('I', 'Y'),
4 => array('M', 'N'),
5 => array('Q', 'U'));
break;
}
// See if $char1 is in a set.
$matchset = array();
for ($i = 0; $i < count($sets); $i++) {
if (in_array($char1, $sets[$i])) {
$matchset = $sets[$i];
}
}
// IF char2 is in the same set as char1, or if char1 and char2 and the same, then return true.
if (in_array($char2, $matchset) OR $char1 == $char2) {
return TRUE;
} else {
return FALSE;
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.soundex.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45843
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45843&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45843&report=yes
Search -- http://master.php.net/manage/user-notes.php