Converting this simple C algorithm to PHP (soundex algorithm)

richardjones
richardjones used Ask the Experts™
on
I have this simple C algorithm to turn a string of text into a soundex code. I need a function to to this in PHP, but i'm not sure where to start. I would have thought that if you know some C and PHP it's not that hard..

<a href="http://west-penwith.org.uk/misc/soundex.htm">Here is a page about soundex</a>

char * Soundex(char * name)
{
        int i, j, k;
        char prev = digit[GROUPS];
        static char out[5];
        char c;
        for (i = 0, j = 0; name[i] != (char)NULL && j < 4; i++)
        {
                c = toupper(name[i]);
                /* decode the character */
                for (k = 0; k < GROUPS; k++)
                        if (strchr(group[k], c) != (char *)NULL)
                        {
                                c = digit[k];
                                break;
                        }
                /* if not found then ignore the character */
                if (k == GROUPS)
                        continue;
                /* to implement the NARA variation include
                if (j != 0 && c == HW)
                        continue;
                */
                /* ignore duplicates */
                if (c != prev)
                {
                        prev = c;
                        /* replace first letter */
                        if (j == 0)
                                c = name[0];
                        /* ignore "vowels" */
                        if (c != '0')
                                out[j++] = c;
                }
        }
        /* padd out to 4 chars */
        for ( ; j < 4; j++)
                out[j] = '0';
        /* terminate the string */
        out[4] = (char)NULL;
        return out;
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What is digit[GROUPS] ?? Have you defined GROUPS somewhere ??

Also I am not sure why you have put up OUT character array of STATIC type ??

Clear out these two doubts and I convert code for you,
Regards,
JD

Commented:
do you think it could help you ?
http://www.php.net/manual/en/function.soundex.php
it should
Commented:
otherwise try this. I didn't try but as you can see PHP
is very similar to C code.


function Soundex(name)
{
       var $i, $j, $k;
       $prev = $digit[$GROUPS];
       $out = array();
       var $c;
       for ($i = 0, $j = 0; $name[$i] != "" && $j < 4; $i++)
       {
               $c = strtoupper($name[$i]);
               /* decode the character */
               for ($k = 0; $k < $GROUPS; $k++)
                       if (strchr($group[$k], $c) != "")
                       {
                               $c = $digit[$k];
                               break;
                       }
               /* if not found then ignore the character */
               if ($k == $GROUPS)
                       continue;
               /* to implement the NARA variation include
               if ($j != 0 && $c == $HW)
                       continue;
               */
               /* ignore duplicates */
               if ($c != $prev)
               {
                       $prev = $c;
                       /* replace first letter */
                       if ($j == 0)
                               $c = $name[0];
                       /* ignore "vowels" */
                       if ($c != '0')
                               $out[$j++] = $c;
               }
       }
       /* padd out to 4 chars */
       for ( ; $j < 4; $j++)
               $out[$j] = '0';
       /* terminate the string */
       $out[4] = "";
       return $out;
}

Author

Commented:
Thanks, i didnt want to use the built-in php soundex function, as i will be tweaking the algorithm.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial