class T9_Exception extends Exception {
}
abstract class T9 {
protected $wordlist = array ( ) ; //
protected $enum = array ( ) ; //
protected $mb_support = true ; //
public function mb_support ( $status = true ) {
$this -> mb_support = $status ;
}
public function __construct ( $filename = null ) {
if ( $filename )
$this -> load ( $filename ) ; //
}
public function load ( $filename ) {
if ( file_exists ( $filename ) && is_readable ( $filename ) ) {
$this -> wordlist = file ( $filename ) ;
//array_walk( $this->wordlist, create_function('&$w', '$w=trim($w);')); //
return ;
}
throw new T9_Exception ( "Can not read $filename " , 100 ) ;
}
/**
* Fetch data from wordlist
* @param string $input
* @return array
*/
public function fetch ( $input ) {
$compare = array ( ) ;
$total = count ( $this -> wordlist ) ;
for ( $i = 0 ; $i < $total ; $i ++ ) {
$len = strlen ( $this -> enum [ $input [ 0 ] ] ) ;
for ( $j = 0 ; $j < $len ; $j ++ ) {
if ( $this -> mb_support == true ) {
if ( mb_strpos ( $this -> wordlist [ $i ] , $this -> enum [ $input [ 0 ] ] [ $j ] ) === 0 )
$compare [ ] = $this -> wordlist [ $i ] ;
}
else {
if ( strpos ( $this -> wordlist [ $i ] , $this -> enum [ $input [ 0 ] ] [ $j ] ) === 0 )
$compare [ ] = $this -> wordlist [ $i ] ;
}
}
}
for ( $i = 1 ; $i < strlen ( $input ) ; $i ++ ) {
$found = false ;
$newcompare = array ( ) ;
for ( $k = 0 ; $k < count ( $compare ) ; $k ++ ) {
for ( $j = 0 ; $j < strlen ( $this -> enum [ $input [ $i ] ] ) ; $j ++ ) {
$letter = $this -> enum [ $input [ $i ] ] [ $j ] ;
if ( $this -> mb_support == true ) {
if ( mb_strtolower ( $compare [ $k ] [ $i ] ) == $letter )
$newcompare [ ] = $compare [ $k ] ;
}
else {
if ( $compare [ $k ] [ $i ] == $letter )
$newcompare [ ] = $compare [ $k ] ;
}
}
}
$compare = $newcompare ;
}
return $compare ;
}
}
class T9_English extends T9 {
protected $enum = array (
0 => '' ,
1 => '' ,
2 => 'abc' ,
3 => 'def' ,
4 => 'ghi' ,
5 => 'jkl' ,
6 => 'mno' ,
7 => 'pqrs' ,
8 => 'tuv' ,
9 => 'wxyz' ,
) ;
}
class T9_Russian extends T9 {
protected $enum = array (
0 => '' ,
1 => '' ,
2 => '' ,
3 => '' ,
4 => '' ,
5 => '' ,
6 => '' ,
7 => '' ,
8 => '' ,
9 => '' ,
) ;
}
Source: https://habr.com/ru/post/86877/
All Articles