ANI - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of ANI.php

  1. <?php
  2.  
  3. use ShortCode\Exception\InputIsTooLarge;
  4. use ShortCode\Exception\UnexpectedCodeLength;
  5.  
  6. abstract class Code
  7. {
  8.     const FORMAT_NUMBER        = '0123456789';
  9.     const FORMAT_ALNUM         = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  10.     const FORMAT_ALNUM_SMALL   = '0123456789abcdefghijklmnopqrstuvwxyz';
  11.     const FORMAT_ALNUM_CAPITAL = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  12.     const FORMAT_CHAR_SMALL    = 'abcdefghijklmnopqrstwxyz';
  13.     const FORMAT_CHAR_CAPITAL  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  14.  
  15.     /**
  16.      * @see http://php.net/manual/en/function.base-convert.php#106546
  17.      *
  18.      * @param $numberInput
  19.      * @param $fromBaseInput
  20.      * @param $toBaseInput
  21.      *
  22.      * @return int|string
  23.      */
  24.     protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput)
  25.     {
  26.         if ($fromBaseInput == $toBaseInput) {
  27.             return $numberInput;
  28.         }
  29.  
  30.         $fromBase  = str_split($fromBaseInput, 1);
  31.         $toBase    = str_split($toBaseInput, 1);
  32.         $number    = str_split($numberInput, 1);
  33.         $fromLen   = strlen($fromBaseInput);
  34.         $toLen     = strlen($toBaseInput);
  35.         $numberLen = strlen($numberInput);
  36.         $retval    = '';
  37.  
  38.         if ($toBaseInput == self::FORMAT_NUMBER) {
  39.             $retval = 0;
  40.             for ($i = 1; $i <= $numberLen; $i++) {
  41.                 $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i)));
  42.             }
  43.             return $retval;
  44.         }
  45.         if ($fromBaseInput != self::FORMAT_NUMBER) {
  46.             $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER);
  47.         } else {
  48.             $base10 = $numberInput;
  49.         }
  50.         if ($base10 < strlen($toBaseInput)) {
  51.             return $base10;
  52.         }
  53.         while ($base10 != '0') {
  54.             $retval = $toBase[bcmod($base10, $toLen)] . $retval;
  55.             $base10 = bcdiv($base10, $toLen, 0);
  56.         }
  57.  
  58.         return $retval;
  59.     }
  60.  
  61.     protected static function getTypeName($value)
  62.     {
  63.         $class = new \ReflectionClass(__CLASS__);
  64.         $constants = array_flip($class->getConstants());
  65.  
  66.         return $constants[$value];
  67.     }
  68. }
  69.  
  70. class Reversible extends Code
  71. {
  72.  
  73.     /**
  74.      * Get a code created from a number
  75.      *
  76.      * @param $input
  77.      * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
  78.      * @param null $minLength
  79.      *
  80.      * @return string
  81.      */
  82.     public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null)
  83.     {
  84.         if(is_int($minLength)) {
  85.             $input += self::getMinForlength($outputFormat, $minLength);
  86.         }
  87.  
  88.         static::throwUnlessAcceptable($outputFormat, $input);
  89.  
  90.         return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat);
  91.     }
  92.  
  93.     /**
  94.      * Revert a code to it's original number
  95.      *
  96.      * @param $input
  97.      * @param string $inputFormat
  98.      * @param null $minLength
  99.      *
  100.      * @return int
  101.      */
  102.     public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null)
  103.     {
  104.         $number = self::convertBase($input, $inputFormat, Code::FORMAT_NUMBER);
  105.  
  106.         if (is_int($minLength)) {
  107.             $number -= self::getMinForlength($inputFormat, $minLength);
  108.         }
  109.  
  110.         return $number;
  111.     }
  112.  
  113.     private static function throwUnlessAcceptable($type, $input)
  114.     {
  115.         if(false !== strpos("$input", 'E+')) {
  116.             throw new InputIsTooLarge("Input is too large to process.");
  117.         }
  118.  
  119.         if($input < 0) {
  120.             throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion.");
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * @param $outputFormat
  126.      * @param $minLength
  127.      *
  128.      * @return int|string
  129.      */
  130.     private static function getMinForlength($outputFormat, $minLength)
  131.     {
  132.         $offset         = str_pad($outputFormat[1], $minLength, $outputFormat[0]);
  133.         $offsetAsNumber = \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER);
  134.         return $offsetAsNumber;
  135.     }
  136.  
  137. }
  138.  
  139. $abc = Reversible::convert('fuck it am gonna leave md5 for now, easy to change on monday when qin comes ad says that he needs to decode it'); 
  140. echo $abc . "\n";
  141.  
  142. $cba = Reversible::revert($abc);
  143. echo $cba . "\n";
  144.  
  145. $abc = Reversible::convert($cba); 
  146. echo $abc . "\n";
File Description
  • ANI
  • PHP Code
  • 16 Jul-2021
  • 4.29 Kb
You can Share it: