Location: PHPKode > scripts > Cifra > cifra/cifra.php
<?php

# cifra class
# coded by Alessandro Rosa
# e-mail : hide@address.com
# site : http://malilla.supereva.it

# Copyright (C) 2006  Alessandro Rosa

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# Compiled with PHP 4.4.0

$oneDIGITarray = array( "0" => "",
                        "1" => "I", 
                        "2" => "II",
                        "3" => "III",
                        "4" => "IV",
                        "5" => "V",
                        "6" => "VI",
                        "7" => "VII",
                        "8" => "VIII",
                        "9" => "IX" ) ;

$twoDIGITarray = array( "0" => "",
                        "1" => "X",
                        "2" => "XX",
                        "3" => "XXX",
                        "4" => "XL",
                        "5" => "L",
                        "6" => "LX",
                        "7" => "LXX",
                        "8" => "LXXX",
                        "9" => "XC" ) ;

$threeDIGITarray = array( "0" => "",
                          "1" => "C", 
                          "2" => "CC",
                          "3" => "CCC",
                          "4" => "CD",
                          "5" => "D",
                          "6" => "DC",
                          "7" => "DCC",
                          "8" => "DCCC",
                          "9" => "CM" ) ;

$fourDIGITarray = array( "0" => "",
                         "1" => "M",
                         "2" => "MM",
                         "3" => "MMM" ) ;

$ALPHANUMERICALarray = array( 0 => "0",
                            1 => "1",
                            2 => "2",
                            3 => "3",
                            4 => "4",
                            5 => "5",
                            6 => "6",
                            7 => "7",
                            8 => "8",
                            9 => "9",
                            10 => "A",
                            11 => "B",
                            12 => "C",
                            13 => "D",
                            14 => "E",
                            15 => "F"
                          ) ;


class cifra
{
    function cifra()
    {
        $this->arabic = "" ;
        $this->roman = "" ;
    }

    function set_arabic( $a )   { $this->arabic = $a ; }

    function pack( $a )
    {
        $packing_zeros = 4 - ( strlen( $a ) % 4 ) ;
        if ( $packing_zeros == 4 ) return $a ;
        
        for ( $i = 0 ; $i < $packing_zeros; $i++ ) $a = "0$a" ;
        
        return $a ;
    }

    function findstr( $strIn, $strQuery )
    {
        $length_in = strlen( $strIn );
        $length_qry = strlen( $strQuery );
    
        for( $i = 0 ; $i <= ( $length_in - $length_qry ); $i++ )
        {
              $strOut = substr( $strIn, $i, $length_qry );
              if ( strcmp( $strQuery, $strOut ) == 0 ) return true ;
        }
    
        return false ;
    }

    
    //////////////////////////////////////////////////////
    function get_arabic()       { return $this->arabic ; }
    function get_roman()        { return $this->roman ;  }

    function format_number( $num, $base )    { return "$num<sub>$base</sub>" ;  }


    function to_roman( $a )
    {
        $this->set_arabic( $a );
        $a = $this->pack( $a ) ;
        
        if ( $a >= 4000 ) return "out of range" ;
        
        $inputDIGIT = "$a" ;
        $inputLEN = strlen( $inputDIGIT );
        
        $a = array();
        
        for ( $i = 0 ; $i < $inputLEN; $i++ )       
        {
            $digit = $inputDIGIT{$i} ;
            
            switch( $i )
            {
                case 3:
                  $a = $GLOBALS['oneDIGITarray'] ;
                break;
                case 2:
                  $a = $GLOBALS['twoDIGITarray'] ;
                break;
                case 1:
                  $a = $GLOBALS['threeDIGITarray'] ;
                break;
                case 0:
                  $a = $GLOBALS['fourDIGITarray'] ;
                break;
                default:
                return "-1";
                break;
            }
            
            $this->roman .= $a["$digit"] ;
        }
        
        return $this->roman ;
    }

    function to_arabic( $a )
    {
        $sum = 0 ;
        ////////////////////////////////////////////////
        $i = 0 ;
        $c = 0 ;
        $digitARRAY = $GLOBALS['fourDIGITarray'] ;
        $token = "";

        foreach( $digitARRAY as $value )
        {
            if ( $this->findstr( $a, $value ) === true && strlen( $value ) >= strlen( $token ) )
            {
                $c = $i ;
                $token = $value ;
            }

            $i++ ;
        }
        
        $s = $c * pow( 10, 3 ) ;
        $sum += $s ;
        $a = substr( $a, strlen( $token ) );
        ////////////////////////////////////////////////
        $i = 0 ;
        $c = 0 ;
        $digitARRAY = $GLOBALS['threeDIGITarray'] ;
        $token = "";

        foreach( $digitARRAY as $value )
        {
            if ( $this->findstr( $a, $value ) === true && strlen( $value ) >= strlen( $token ) )
            {
                $c = $i ;
                $token = $value ;
            }

            $i++ ;
        }
        
        $s = $c * pow( 10, 2 ) ;
        $sum += $s ;
        $a = substr( $a, strlen( $token ) );
        ////////////////////////////////////////////////
        $i = 0 ;
        $c = 0 ;
        $digitARRAY = $GLOBALS['twoDIGITarray'] ;
        $token = "";

        foreach( $digitARRAY as $value )
        {
            if ( $this->findstr( $a, $value ) === true && strlen( $value ) >= strlen( $token ) )
            {
                $c = $i ;
                $token = $value ;
            }

            $i++ ;
        }
        
        $s = $c * pow( 10, 1 ) ;
        $sum += $s ;
        $a = substr( $a, strlen( $token ) );
        ////////////////////////////////////////////////
        $i = 0 ;
        $c = 0 ;
        $digitARRAY = $GLOBALS['oneDIGITarray'] ;
        $token = "";

        foreach( $digitARRAY as $value )
        {
            if ( strcmp( $a, $value ) == 0  && strlen( $value ) >= strlen( $token ) )
            {
                $c = $i ;
                $token = $value ;
            }

            $i++ ;
        }
        
        $s = $c * pow( 10, 0 ) ;
        $sum += $s ;
        $a = substr( $a, strlen( $token ) );
        ////////////////////////////////////////////////
        $this->arabic = $sum ;
        return $sum ;
    }
    
    function to_base( $inputNUM, $base )
    {
          if ( $base < 2 || $base > 16 ) return "NO BASE ALLOWED !" ;
          
          $ret_str = "" ;
          
          $ALPHANUMarray = $GLOBALS['ALPHANUMERICALarray'] ;
          
          while( $inputNUM >= $base )
          {
                $remainder = $inputNUM % $base ;
                $digit = $ALPHANUMarray[$remainder];
                $ret_str = "$digit" . $ret_str ;
                $inputNUM = ( $inputNUM - $remainder ) / $base ;
          }
    
          $digit = $ALPHANUMarray[$inputNUM];
          $ret_str = "$digit" . $ret_str ;
          
          $ret_str = $this->pack( $ret_str ) ;
          
          return $ret_str ;
    }

    function to_decimal( $inputNUM, $base )
    {
        $sum = 0 ;
        $len = strlen( $inputNUM );
        
        for ( $i = $len - 1; $i >= 0 ; $i-- )
        {
            $c = $inputNUM{$i} + 0;
            $exponent = $len - 1 - $i ;
            $sum += $c * pow( $base, $exponent );
        }
        
        return $sum ;
    }
    
    
    ///////////// CLASS MEMBERS //////////////////////////
    
    var $arabic ;
    var $roman ;
}

?>
Return current item: Cifra