How do I remove the characters from variables in PHP to send to the bank? [closed]

-1
How do I remove the ".", "-", "/", "(" and ")" characters of the PHP variables to send to the database?

<?php
    session_start();
    include("_cabecalho.php");
    include("_bUsuario.php");

    $btnCad = filter_input(INPUT_POST, 'btnCad', FILTER_SANITIZE_STRING);

    if ($btnCad) {

        $nome = $_POST['nome'];
        $email = $_POST['email'];
        $cpf = $_POST['cpf'];
        $cpfBd = explode('.', $cpf);
        $cpfBd = explode('-', $cpfBd);
        $login = $_POST['login'];
        $senha1 = $_POST['senha1'];
        $senha = password_hash($senha1, PASSWORD_DEFAULT);
        $celular = $_POST['celular'];
        $celularBd = explode('(', $celular);
        $celularBd = explode(')', $celularBd);
        $celularBd = explode('-', $celularBd);
        $telefone = $_POST['telefone'];
        $telefoneBd = explode('(', $telefone);
        $telefoneBd = explode(')', $telefoneBd);
        $telefoneBd = explode('-', $telefoneBd);
        $data = $_POST['dtn'];
        $dataBd = explode('/', $data);
        $sexo = $_POST['sexo'];
        $cep = $_POST['cep'];
        $cepBd = explode('-', $cep);
        $rua = $_POST['rua'];
        $numero = $_POST['numero'];
        $bairro = $_POST['bairro'];
        $cidade = $_POST['cidade'];
        $estado = $_POST['uf'];

        if (insereUsuario($conexao, $nome, $email, $login, $senha, $celularBd, $telefoneBd, $sexo, $cepBd, $rua, $numero, $bairro, $cidade, $estado, $cpfBd, $dataBd)) {
            ?>
            <p class="center green-text">O Usuario <?= $login ?> foi adicionado.</p>
            <?php 
        } 
        else {
            $msg = mysqli_error($conexao);
            ?>
            <p class="center red-text">O Usuario <?= $login ?> não foi adicionado: <?= $msg ?></p>
            <?php
        }

<?php
    include("conecta.php");

    function insereUsuario($conexao, $nome, $email, $login, $senha, $celularBd, $telefoneBd, $sexo, $cepBd, $rua, $numero, $bairro, $cidade, $estado, $cpfBd, $dataBd) {
        $query = "insert into usuarios (nome, email, login, senha, celular, telefone, sexo, cep, rua, numero, bairro, cidade, estado, cpf, datanascimento) 
                 values ('{$nome}', '{$email}', '{$login}', '{$senha}', {$celularBd},   {$telefoneBd}, '{$sexo}', {$cepBd}, '{$rua}', {$numero}, '{$bairro}', '{$cidade}', '{$estado}', {$cpfBd}, {$dataBd})";
        return mysqli_query($conexao, $query);
    }

And you're giving this error here in the browser

    
asked by anonymous 20.10.2017 / 06:46

3 answers

3

1 - The function explode() does not serve to remove characters, its function is to explode a string separating its value in an array according to the chosen parameter, as in the following example:

$string = 'a|b|c|d';
$arr = explode('|',$string);
//O exemplo retorna
$arr[0] = 'a';
$arr[1] = 'b';
$arr[2] = 'c';
$arr[3] = 'd';

2 - To remove special characters from a string, as in your case, you need to remove '.' and '-' use the str_replace() function, as in the following example:

$cepBd = str_replace("-", "", $cep);

The function str_replace() means:

str_replace('caractere a ser substituido','caractere que vai substituir (pode ser vazio)', 'string que deverá ser alterada')

PS: There is more than one function with result similar to its particularities and mode of use, below for consultation:

str_ireplace ();

strstr ();

substr_replace ();

preg_replace ();

    
20.10.2017 / 12:42
0

The explode() asks for 3 parameters the third one that is missing is the maximum number of elements in the array, remembering that the explode generates an array. In your case I believe that what you need is the 0 in case 1 element for example:

$str = explode('/', $_POST['str'], 0);
    
20.10.2017 / 07:05
0
  • Replacing strings is a very common operation, but not always done properly.

  • The simplest function is str_replace , which is used to replace all occurrences of one string with another within a string.

  • When you want to replace multiple character strings with other strings at once, it may be more useful to use the function by passing two arrays (one with the sequences to be fetched and one with the sequences used to replace them) / p>

Example:

$str = '(21)1234-5678';
$substituirIsso = array('-', ')', '(');
$porIsso = array('');
$resultado = str_replace($substituirIsso, $porIsso, $str);

result - ideone

  

The advantage of using the array instead of calling str_replace two or more times is that the string only needs to be traversed once.

In your case it would look like this:

Example - ideone

function removeCaracteres($str){
    return str_replace(array(".", "-", "/", "(", ")"), "", $str);
}

    $nome = $_POST['nome'];
    $email = $_POST['email'];

    $cpf = $_POST['cpf'];
    $cpfBd = removeCaracteres($cpf);

    $login = $_POST['login'];
    $senha1 = $_POST['senha1'];
    $senha = password_hash($senha1, PASSWORD_DEFAULT);

    $celular = $_POST['celular'];
    $celularBd = removeCaracteres($celular);

    $telefone = $_POST['telefone'];
    $telefoneBd = removeCaracteres($telefone);

    $data = $_POST['dtn'];
    $dataBd = removeCaracteres($data);

    $sexo = $_POST['sexo'];

    $cep = $_POST['cep'];
    $cepBd = removeCaracteres($cep);

    $rua = $_POST['rua'];
    $numero = $_POST['numero'];
    $bairro = $_POST['bairro'];
    $cidade = $_POST['cidade'];
    $estado = $_POST['uf'];
    
21.10.2017 / 01:34