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.



Your result can be seen below.

Result of php executing





Full code of Teste Encode BEST.php

  1. <?php
  2.  
  3.  
  4. function bestSignEncode(string $name, $email=null, $tel=null){
  5.         $array = [$name, $email, $tel];                                         // monto um array com parâmetros
  6.         $json = json_encode($array);                                            // converto o array para json
  7.         $base64 = base64_encode($json);                                         // encode o json pra base64
  8.         $special = strtr($base64,['+'=>'-','/'=>',','=='=>';','='=>':']);       // substituo os caracteres da string (+ por - ... / por , ... == por ; ... = por :)
  9.         $array = str_split($special);                                           // monto um array de caracteres da string
  10.         $even = '';
  11.         $odd = '';
  12.         foreach ($array as $k => $v){                                           // percorro um laço com a array de caracteres 
  13.                 if ($k === 0 || $k % 2 == 0) $even .= $v;                           // adiciono o caracter na coleção par
  14.                 else $odd .= $v;                                                    // adiciono o caracter na coleção impar
  15.         }
  16.         return $odd.'_'.$even;                                                  // retorno impar e par concatenados por um underscore
  17. }
  18.  
  19.  
  20. function bestSignDecode(string $str){
  21.         $oe = explode('_', $str); 
  22.         $odd = $oe[0];
  23.         $even = $oe[1];
  24.         $length = max(strlen($odd),strlen($even));
  25.         $special = '';
  26.         for ($i=0; $i<$length; $i++){
  27.                 $special .= $even[$i] . $odd[$i];
  28.         }
  29.         $base64 = strtr($special,['-'=>'+',','=>'/',':'=>'=',';'=>'==']);
  30.         return base64_decode($base64);
  31. }
  32.  
  33. $encoded = bestSignEncode('BEST','[email protected]','123456');
  34.  
  35. echo 'ENCODED: ' . $encoded;
  36.  
  37. echo "\n";
  38.  
  39. $decoded = bestSignDecode($encoded);
  40. echo 'DECODED: ' . $decoded;
  41.  
File Description
  • Teste Encode BEST
  • PHP Code
  • 05 Feb-2021
  • 1.62 Kb
You can Share it: