PHP Classes
Icontem

File: villes/php4/soundex2.cls.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Johan Barbier  >  Cities and zip codes  >  villes/php4/soundex2.cls.php  
File: villes/php4/soundex2.cls.php
Role: Class source
Content type: text/plain
Description: Soundex2 class
Class: Cities and zip codes
Lookup for cities and zip codes using AJAX
 

Contents

Class file image Download
<?php
/**
* CLASS soundex2
* soundex2 French version
* based on the algorithm described here : http://sqlpro.developpez.com/cours/soundex/ by Frédéric BROUARD
*
* author Johan Barbier <barbier_johan@hotmail.com>
*/
class soundex2 {

    
/**
    * public sString
    * main string we work on
    */
    
var $sString '';

    
/**
    * vowels replacement array
    */
    
var $aReplaceVoy1 = array (
        
'E' => 'A',
        
'I' => 'A',
        
'O' => 'A',
        
'U' => 'A'
    
);

    
/**
    * consonnants replacement array
    */
    
var $aReplaceGrp1 = array (
        
'GUI' => 'KI',
        
'GUE' => 'KE',
        
'GA' => 'KA',
        
'GO' => 'KO',
        
'GU' => 'K',
        
'CA' => 'KA',
        
'CO' => 'KO',
        
'CU' => 'KU',
        
'Q' => 'K',
        
'CC' => 'K',
        
'CK' => 'K'
        
);

    
/**
    * other replacement array
    */
    
var $aReplaceGrp2 = array (
        
'ASA' => 'AZA',
        
'KN' => 'NN',
        
'PF' => 'FF',
        
'PH' => 'FF',
                
'SCH' => 'SSS'
        
);

    
/**
    * endings replacement array
    */
    
var $aEnd = array (
        
'A',
        
'T',
        
'D',
        
'S'
        
);

    
/**
    * public function build
    * core function of the class, go through the whole process
    * @Param string sString : the string we want to check
    */
    
function build ($sString) {
        
/**
        * let's check it's a real string...
        */
        
if (is_string ($sString) && !empty ($sString)) {
            
$this -> sString $sString;
        } else {
            
trigger_error ('Parameter string must not be empty'E_USER_ERROR);
        }
        
/**
        * remove starting and ending spaces
        */
        
$this -> sString trim ($this -> sString);
        
/**
        * remove special french characters
        */
        
$this -> trimAccent ();
        
/**
        * string to upper case
        */
        
$this -> sString strtoupper ($this -> sString );
        
/**
        * let's remove every space in the string
        */
        
$this -> sString str_replace (' '''$this -> sString);
        
/**
        * let's remove every '-' in the string
        */
        
$this -> sString str_replace ('-'''$this -> sString);
        
/**
        * let's process through the first replacement array
        */
        
$this -> arrReplace ($this -> aReplaceGrp1);
        
/**
        * let's process through th vowels replacement
        */
        
$sChar substr ($this -> sString01);
        
$this -> sString substr ($this -> sString1strlen ($this -> sString) - 1);
        
$this -> arrReplace ($this -> aReplaceVoy1);
        
$this -> sString $sChar.$this -> sString;
        
/**
        * let's process through the second replacement array
        */
        
$this -> arrReplace ($this -> aReplaceGrp2true);
        
/**
        * let's remove every 'H' but those prededed by a 'C' or an 'S'
        */
        
$this -> sString preg_replace ('/(?<![CS])H/'''$this -> sString);
        
/**
        * let's remove every 'Y' but those preceded by an 'A'
        */
        
$this -> sString preg_replace ('/(?<!A)Y/'''$this -> sString);
        
/**
        * remove endings in aEnd
        */
        
$length strlen ($this -> sString) - 1;
        if (
in_array ($this -> sString{$length}, $this -> aEnd)) {
            
$this -> sString substr ($this -> sString0$length);
        }
        
/**
        * let's remove every 'A', but the one at the beginning of the string, if any.
        */
        
$sChar '';
        if (
$this -> sString{0} === 'A') {
            
$sChar 'A';
        }
        
$this -> sString str_replace ('A'''$this -> sString);
        
$this -> sString $sChar.$this -> sString;
        
/**
        * let's have only 1 occurence of each letter
        */
        
$this -> sString preg_replace'/(.)\1/''$1'$this -> sString );
        
/**
        * let's have the final code : a 4 letters string
        */
        
$this -> getFinal ();
    }

    
/**
    * private function getFinal
    * gets the first 4 letters, pads the string with white space if the string length < 4
    */
    
function getFinal () {
        if (
strlen ($this -> sString) < 4) {
            
$this -> sString str_pad ($this -> sString4' 'STR_PAD_RIGHT);
        } else {
            
$this -> sString substr ($this -> sString04);
        }
    }

    
/**
    * private function trimAccent
    * remove every special French letters
    */
    
function trimAccent () {
       
$this -> sString htmlentities(strtolower($this -> sString ));
       
$this -> sString preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml|grave);/""$1"$this -> sString );
       
$this -> sString preg_replace("/([^a-z0-9]+)/""-"html_entity_decode($this -> sString ));
       
$this -> sString trim($this -> sString "-");
    }

    
/**
    * private function arrReplace
    * replacement method, given an array
    * @Param array tab : the replacement array to be used
    * @Param bool pref : if false, just replace keys by values; if true, do the same but only with prefix
    */
    
function arrReplace ($tab$pref false) {
        
$fromRep array_keys ($tab);
        
$toRep array_values ($tab);
        if (
false === $pref) {
            
$this -> sString str_replace ($fromRep$toRep$this -> sString);
        } else {
            foreach (
$fromRep as $clef => $val) {
                
$length strlen ($val);
                if (
substr ($this -> sString0$length) === $val) {
                    
$this -> sString substr_replace ($this -> sString$toRep[$clef], 0$length);
                }
            }
        }
    }

}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products