36 || $destBase < 2 || $destBase > 36 || $sourceBase != (int)$sourceBase || $destBase != (int)$destBase || $pad != (int)$pad || !preg_match( "/^[" . substr( '0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase ) . "]+$/i", $input ) ) { return false; } static $baseChars = [ 10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f', 16 => 'g', 17 => 'h', 18 => 'i', 19 => 'j', 20 => 'k', 21 => 'l', 22 => 'm', 23 => 'n', 24 => 'o', 25 => 'p', 26 => 'q', 27 => 'r', 28 => 's', 29 => 't', 30 => 'u', 31 => 'v', 32 => 'w', 33 => 'x', 34 => 'y', 35 => 'z', '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29, 'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35 ]; if ( extension_loaded( 'gmp' ) && ( $engine == 'auto' || $engine == 'gmp' ) ) { $result = gmp_strval( gmp_init( $input, $sourceBase ), $destBase ); } elseif ( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) { $decimal = '0'; foreach ( str_split( strtolower( $input ) ) as $char ) { $decimal = bcmul( $decimal, $sourceBase ); $decimal = bcadd( $decimal, $baseChars[$char] ); } // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed for ( $result = ''; bccomp( $decimal, 0 ); $decimal = bcdiv( $decimal, $destBase, 0 ) ) { // As of PHP 7.2, bcmod can return a floating point value if bcscale is nonzero $result .= $baseChars[(int)bcmod( $decimal, $destBase )]; } // @codingStandardsIgnoreEnd $result = strrev( $result ); } else { $inDigits = []; foreach ( str_split( strtolower( $input ) ) as $char ) { $inDigits[] = $baseChars[$char]; } // Iterate over the input, modulo-ing out an output digit // at a time until input is gone. $result = ''; while ( $inDigits ) { $work = 0; $workDigits = []; // Long division... foreach ( $inDigits as $digit ) { $work *= $sourceBase; $work += $digit; if ( $workDigits || $work >= $destBase ) { $workDigits[] = (int)( $work / $destBase ); } $work %= $destBase; } // All that division leaves us with a remainder, // which is conveniently our next output digit. $result .= $baseChars[$work]; // And we continue! $inDigits = $workDigits; } $result = strrev( $result ); } if ( !$lowercase ) { $result = strtoupper( $result ); } return str_pad( $result, $pad, '0', STR_PAD_LEFT ); }