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.
Result of php executing
Full code of ANI.php
- <?php
- use ShortCode\Exception\InputIsTooLarge;
- use ShortCode\Exception\UnexpectedCodeLength;
- abstract class Code
- {
- const FORMAT_NUMBER = '0123456789';
- const FORMAT_ALNUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- const FORMAT_ALNUM_SMALL = '0123456789abcdefghijklmnopqrstuvwxyz';
- const FORMAT_ALNUM_CAPITAL = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- const FORMAT_CHAR_SMALL = 'abcdefghijklmnopqrstwxyz';
- const FORMAT_CHAR_CAPITAL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- /**
- * @see http://php.net/manual/en/function.base-convert.php#106546
- *
- * @param $numberInput
- * @param $fromBaseInput
- * @param $toBaseInput
- *
- * @return int|string
- */
- protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput)
- {
- if ($fromBaseInput == $toBaseInput) {
- return $numberInput;
- }
- $fromBase = str_split($fromBaseInput, 1);
- $toBase = str_split($toBaseInput, 1);
- $number = str_split($numberInput, 1);
- $fromLen = strlen($fromBaseInput);
- $toLen = strlen($toBaseInput);
- $numberLen = strlen($numberInput);
- $retval = '';
- if ($toBaseInput == self::FORMAT_NUMBER) {
- $retval = 0;
- for ($i = 1; $i <= $numberLen; $i++) {
- $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i)));
- }
- return $retval;
- }
- if ($fromBaseInput != self::FORMAT_NUMBER) {
- $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER);
- } else {
- $base10 = $numberInput;
- }
- if ($base10 < strlen($toBaseInput)) {
- return $base10;
- }
- while ($base10 != '0') {
- $retval = $toBase[bcmod($base10, $toLen)] . $retval;
- $base10 = bcdiv($base10, $toLen, 0);
- }
- return $retval;
- }
- protected static function getTypeName($value)
- {
- $class = new \ReflectionClass(__CLASS__);
- $constants = array_flip($class->getConstants());
- return $constants[$value];
- }
- }
- class Reversible extends Code
- {
- /**
- * Get a code created from a number
- *
- * @param $input
- * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
- * @param null $minLength
- *
- * @return string
- */
- public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null)
- {
- if(is_int($minLength)) {
- $input += self::getMinForlength($outputFormat, $minLength);
- }
- static::throwUnlessAcceptable($outputFormat, $input);
- return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat);
- }
- /**
- * Revert a code to it's original number
- *
- * @param $input
- * @param string $inputFormat
- * @param null $minLength
- *
- * @return int
- */
- public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null)
- {
- $number = self::convertBase($input, $inputFormat, Code::FORMAT_NUMBER);
- if (is_int($minLength)) {
- $number -= self::getMinForlength($inputFormat, $minLength);
- }
- return $number;
- }
- private static function throwUnlessAcceptable($type, $input)
- {
- if(false !== strpos("$input", 'E+')) {
- throw new InputIsTooLarge("Input is too large to process.");
- }
- if($input < 0) {
- throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion.");
- }
- }
- /**
- * @param $outputFormat
- * @param $minLength
- *
- * @return int|string
- */
- private static function getMinForlength($outputFormat, $minLength)
- {
- $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]);
- $offsetAsNumber = \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER);
- return $offsetAsNumber;
- }
- }
- $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');
- echo $abc . "\n";
- $cba = Reversible::revert($abc);
- echo $cba . "\n";
- $abc = Reversible::convert($cba);
- echo $abc . "\n";
File Description
- ANI
- PHP Code
- 16 Jul-2021
- 4.29 Kb
You can Share it:
Latest PHP Pastes