TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-09-2009, 06:36 PM   #1 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default SoundEX Algorithm: Knuth

Well I needed to employ the soundex algorithm for some simple search techniques in a script I'm writing and without doing research I started writing my own class to calculate the soundex...and than found the soundex function when I typed out SoundEX into my IDE...So I figured I'd atleast post it up here to share with people who'd be interested.

php Code:
<?php

class SoundEX
{
   
    private static function encode( $String )
    {
        switch( strtolower($String) )
        {
            case 'b':
            case 'f':
            case 'p':
            case 'v':
                return "1";
            case 'c':
            case 'g':
            case 'j':
            case 'k':
            case 'q':
            case 's':
            case 'x':
            case 'z':
                return "2";
            case 'd':
            case 't':
                return "3";
            case 'l':
                return "4";
            case 'm':
            case 'n':
                return "5";
            case 'r':
                return "6";
            default:
                return null;
        }
    }
   
        //As described by Knuth
    public static function generateSoundEX( $String )
    {
        if( strlen($String) > 0  )
        {
            $String = strtolower($String);
            $outPut .= $String{0};
           
            for( $i = 1; $i < strlen($String) && strlen($outPut) < 4; $i++ )
            {
                $c = self::encode($String{$i});
               
                switch( $String{$i -1 } )
                {
                    case 'a':
                        case 'e':
                        case 'i':
                        case 'o':
                        case 'u':
                            $outPut .= $c;
                            break;
                        case 'h':
                        case 'w':
                        default:
                            if(strlen($outPut) == 1 )
                            {
                                if(self::encode($outPut[strlen($outPut)-1]) != $c)
                                    $outPut .= $c;
                            } else {
                                if($outPut[strlen($outPut)-1] != $c)
                                    $outPut .= $c;
                            }
                           
                            break;
                }
            }
           
            for( $i = strlen($outPut); $i < 4; ++$i )
            {
                $outPut .= "0";
            }
           
            return (string)$outPut;
        }
    }
}
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pixelate algorithm using GD Kalle Script Giveaway 9 06-27-2009 12:36 AM
Google-News-Like Headline Grouping Algorithm serversphere Advanced PHP Programming 0 02-29-2008 05:21 PM
hash() algorithm info script RobertK Script Giveaway 4 01-09-2008 03:00 PM


All times are GMT. The time now is 03:50 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design