Teste Encode BEST - 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 Teste Encode BEST.php
- <?php
- function bestSignEncode(string $name, $email=null, $tel=null){
- $array = [$name, $email, $tel]; // monto um array com parâmetros
- $json = json_encode($array); // converto o array para json
- $base64 = base64_encode($json); // encode o json pra base64
- $special = strtr($base64,['+'=>'-','/'=>',','=='=>';','='=>':']); // substituo os caracteres da string (+ por - ... / por , ... == por ; ... = por :)
- $array = str_split($special); // monto um array de caracteres da string
- $even = '';
- $odd = '';
- foreach ($array as $k => $v){ // percorro um laço com a array de caracteres
- if ($k === 0 || $k % 2 == 0) $even .= $v; // adiciono o caracter na coleção par
- else $odd .= $v; // adiciono o caracter na coleção impar
- }
- return $odd.'_'.$even; // retorno impar e par concatenados por um underscore
- }
- function bestSignDecode(string $str){
- $oe = explode('_', $str);
- $odd = $oe[0];
- $even = $oe[1];
- $length = max(strlen($odd),strlen($even));
- $special = '';
- for ($i=0; $i<$length; $i++){
- $special .= $even[$i] . $odd[$i];
- }
- $base64 = strtr($special,['-'=>'+',','=>'/',':'=>'=',';'=>'==']);
- return base64_decode($base64);
- }
- echo 'ENCODED: ' . $encoded;
- echo "\n";
- $decoded = bestSignDecode($encoded);
- echo 'DECODED: ' . $decoded;