How do I remove the characters from the variables in PHP to send to the database?

Asked

Viewed 423 times

-1

How I remove the characters "." "-", "/", "(" and ")" from the variables in PHP 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 this is giving this error here in the browser erro pagina do navegador

  • Hello friend. You should not attach code images. This is clear in the community rules. I suggest you edit your answer and attach the code typed as soon as possible or it will be negativated by the other users. Hugs!

  • I edited the first image for code, please do the same in the second image.

  • Sorry I didn’t know

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 parameter chosen, 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 function str_replace(), 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, follows below for query:

str_ireplace();

strstr();

substr_replace();

preg_replace();

0

  • String replacement is a very common operation, but not always done properly.

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

  • When you want to replace several strings with others at once, it may be more useful to use the function by passing two arrays (one with sequences to be searched and the other with sequences used to replace).

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 twice or more times the string only needs to be run 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'];

0

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

$str = explode('/', $_POST['str'], 0);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.