Ir para conteúdo
Fórum Script Brasil
  • 0

str_replace não está funcionando


Leandro Aparecido

Pergunta

Boa noite!

Estou fazendo um sistema bem simples de criptografia, estou usando a função str_replace, porém o resultado é muito estranho não substitui corretamente, já tentei diversas alternativas mas sem sucesso, alguém poderia me dar uma dica, segue o código abaixo:

<html>
    <head>
        <title>Sistema de Criptografar</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="estilo.css">
    </head>
    <body>
    <center>
    <form  action="" method="POST" >
 
        <h1>Sistema de Criptografia</h1><br/>
 
            <label>Digite a Chave: </label><br/>
            <input type="text" class='texto' name="chave" value="" /><br/><br/>
            
            <label>Digite o texto:</label><br/>
            <textarea name="texto" rows="0" cols="0"></textarea><br/><br/> 
 
            <input type="submit" value="Criptografar" name="enviar" class="submit" /> 
            <input type="submit" value="Descriptografar" name="enviar" class="submit" />      
       
    </form>
    </center>
        <?php
        
            
            if(isset($_POST["enviar"])){
            
            $texto=$_POST['texto'];
            $chave=$_POST['chave'];
            
            $seg=123;
            
            $criptoalterar =  array ("a","b","c","d","e","f","g","h","i","j","l","m","n","o","p","q","r","s","t","u","v","x","z");
            $criptoalterado = array ("u","n","r","o","p","t","6","y","k","w","q","9","5","v","6","c","0","x","h","3","b","7","f");
            
            if ($chave== "") {
               
                echo '<center class="format">Campo chave é obrigatório</center>';            
                exit();
            }
            if ($chave!=$seg){
                  
                   echo 'Chave Incorreta Verifique e tente novamente!';
                   exit();

           }
           
           switch ($_POST['enviar']){
           case 'Criptografar':
              
            $alterar = str_replace($criptoalterar, $criptoalterado, $texto);
            echo $alterar;
                
            break;

           case 'Descriptografar':
               
            
             $voltar = str_replace($criptoalterado, $criptoalterar, $texto);
           
             echo $voltar;
           
            break;
           default:
          }
       }
       ?>       
            
    </body>
 </html>

 

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Boa noite!

Estou fazendo um sistema bem simples de criptografia, estou usando a função str_replace, porém o resultado é muito estranho não substitui corretamente, já tentei diversas alternativas mas sem sucesso, alguém poderia me dar uma dica, segue o código abaixo:

<html>
    <head>
        <title>Sistema de Criptografar</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="estilo.css">
    </head>
    <body>
    <center>
    <form  action="" method="POST" >
 
        <h1>Sistema de Criptografia</h1><br/>
 
            <label>Digite a Chave: </label><br/>
            <input type="text" class='texto' name="chave" value="" /><br/><br/>
            
            <label>Digite o texto:</label><br/>
            <textarea name="texto" rows="0" cols="0"></textarea><br/><br/> 
 
            <input type="submit" value="Criptografar" name="enviar" class="submit" /> 
            <input type="submit" value="Descriptografar" name="enviar" class="submit" />      
       
    </form>
    </center>
        <?php
        
            
            if(isset($_POST["enviar"])){
            
            $texto=$_POST['texto'];
            $chave=$_POST['chave'];
            
            $seg=123;
            
            $criptoalterar =  array ("a","b","c","d","e","f","g","h","i","j","l","m","n","o","p","q","r","s","t","u","v","x","z");
            $criptoalterado = array ("u","n","r","o","p","t","6","y","k","w","q","9","5","v","6","c","0","x","h","3","b","7","f");
            
            if ($chave== "") {
               
                echo '<center class="format">Campo chave é obrigatório</center>';            
                exit();
            }
            if ($chave!=$seg){
                  
                   echo 'Chave Incorreta Verifique e tente novamente!';
                   exit();

           }
           
           switch ($_POST['enviar']){
           case 'Criptografar':
              
            $alterar = str_replace($criptoalterar, $criptoalterado, $texto);
            echo $alterar;
                
            break;

           case 'Descriptografar':
               
            
             $voltar = str_replace($criptoalterado, $criptoalterar, $texto);
           
             echo $voltar;
           
            break;
           default:
          }
       }
       ?>       
            
    </body>
 </html>

 

<html>
    <head>
        <title>Sistema de Criptografar</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="estilo.css">
    </head>
    <body>
    <center>
    <form  action="" method="POST" >
 
        <h1>Sistema de Criptografia</h1><br/>
 
            <label>Digite a Chave: </label><br/>
            <input type="text" class='texto' name="chave" value="" /><br/><br/>
            
            <label>Digite o texto:</label><br/>
            <textarea name="texto" rows="0" cols="0"></textarea><br/><br/> 
 
            <input type="submit" value="Criptografar" name="enviar" class="submit" /> 
            <input type="submit" value="Descriptografar" name="enviar" class="submit" />      
       
    </form>
    </center>
        <?php
        
            
            if(isset($_POST["enviar"])){
            
            $texto=$_POST['texto'];
            $chave=$_POST['chave'];
            
            $seg=123;
            
            $criptoalterar =  array ("a","b","c","d","e","f","g","h","i","j","l","m","n","o","p","q","r","s","t","u","v","x","z");
            $criptoalterado = array ("U","N","R","O","P","T","6","Y","K","W","Q","9","5","V","6","C","0","X","H","3","B","7","F");
            
            if ($chave== "") {
               
                echo '<center class="format">Campo chave é obrigatório</center>';            
                exit();
            }
            if ($chave!=$seg){
                  
                   echo 'Chave Incorreta Verifique e tente novamente!';
                   exit();

           }
           
           switch ($_POST['enviar']){
           case 'Criptografar':
              
            $alterar = str_replace($criptoalterar, $criptoalterado, $texto);
            echo $alterar;
                
            break;

           case 'Descriptografar':
               
            
             $voltar = str_replace($criptoalterado, $criptoalterar, $texto);
           
             echo $voltar;
           
            break;
           default:
          }
       }
       ?>       
            
    </body>
 </html>

Olá, tente desta forma!

Funcionou perfeitamente, porém mudei de minuscula para maiúscula. : ]

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,8k
×
×
  • Criar Novo...