soundex 


string apg

CALCULATE the soundex key of a STRING.





This function has the purpose of calculating a four-character string as described by Donald Knuth in "The Art of Computer Programming, whose purpose is to "measure" their similarity of pronunciation.



<?php

str soundex 
str $str )


where,

$str The STRING to calcule the soundex

?>
 

$string


The STRING to get the soundex.



  1 EXERCISE   

<?php

$arrstr01 
= [ 'widow''window'];

foreach(
$arrstr01 as $str01 => $s01)
{
    echo 
'[ ' $str01 ' ]&nbsp;' 
                  
$s01 ' => ' soundex($s01) . '<br>';
}

?> 

 RESULT   

[ 0 ] widow => W300
[ 1 ] window => W530


  2 EXERCISE   

<?php

$arrstr02 
= [ 'Doc',  'Grumpy',  
                                
'Happy',  'Sleepy',  
                              
'Bashful',  'Sneezy',  'Dopey' ];

foreach(
$arrstr02 as $str02 => $s02)
{
    echo 
'[ ' $str02 ' ]&nbsp;' 
                  
$s02 ' => ' soundex($s02) . '<br>';
}

?> 

 RESULT   

[ 0 ] Doc => D200
[ 1 ] Grumpy => G651
[ 2 ] Happy => H100
[ 3 ] Sleepy => S410
[ 4 ] Bashful => B214
[ 5 ] Sneezy => S520
[ 6 ] Dopey => D100


  3 EXERCISE   

<?php

/* Brazilian seven dwarfs names */

$arrstr03 = [ 'Soneca''Dengoso'
                                          
'Feliz''Atchim'
                                      
'Mestre''Zangado''Dunga' ];

foreach(
$arrstr03 as $str03 => $s03)
{
    echo 
'[ ' $str03 ' ]&nbsp;' 
                    
$s03 ' => ' soundex($s03) . '<br>';
}

?> 

 RESULT   

[ 0 ] Soneca => S520
[ 1 ] Dengoso => D522
[ 2 ] Feliz => F420
[ 3 ] Atchim => A325
[ 4 ] Mestre => M236
[ 5 ] Zangado => Z523
[ 6 ] Dunga => D520


  4 EXERCISE   

<?php

echo "soundex(\"\") = " soundex("");

echo 
"<br>soundex(-1) = " soundex(-1);

echo 
'<br><br>';

$array = [
"From",
"that",
"time",
"on",
"Sam",
"thought",
"that",
"he",
"sensed",
"a",
"change",
"in",
"Gollum",
"again.",
"He was more fawning and would-be friendly; 
but Sam surprised some strange looks in 
his eyes at times, especially towards Frodo."
];

foreach (
$array as $k => $v) {
echo 
"[ $k ] $v => " soundex($v) . "<br>";
}


  5 EXERCISE   

<?php

echo "Testing soundex() :<br>
                basic functionality.<br><br>"
;

$arr05a = [ "Euler""Gaus""Hilbert"
                           
"Knuth""Lloyd""Lukasiewicz" ];

foreach(
$arr05a as $k05a => $v05a) {
echo 
"[ $k05a ] $v05a => " soundex($v05a) . "<br>"; }
    
echo 
'<br><br>';

$arr05b = [ "Ellery""Gosh""Heilbronn"
                                  
"Kant""Ladd""Lissajous" ];

foreach(
$arr05b as $k05b => $v05b) {
echo 
"[ $k05b ] $v05b => " soundex($v05b) . "<br>"; }

for(
$i05 0$i05 >= 5$i05++) {
    if(
soundex($arr05a[$i05]) == soundex($arr05b[$i05])) {
        echo 
"$arr05a[$i05] ==> $arr05b[$i05] => " 
                           
soundex($arr05a[$i05]) . "<br>";
    }
    else
    {
        echo 
"#<br>";
    }
}
    

?>