How do you compare a string when it only has one space?

Asked

Viewed 98 times

1

I would like to compare my variables, to validate a form:

if ($nome or $categoria or $rua or $numero or $bairro or $abre or $fecha or $email or $telefone == " ") {           
    echo"Tente novamente</b>, faltou preencher corretamente um ou mais campos do formulário.";
}

However, the echo within the code is not executed. I discovered that one or more of these variables contains a blank!

How can I correct this validation?

  • It’s not very clear what your doubt is

  • Now the question got weird, I wanted to compare variable by variable. That was the question. @dvd solved.

2 answers

4

This is simple to solve, just leave only the quotes without the space and put the variables inside the function Trim (denied):

if (!trim($nome) or !trim($categoria) or !trim($rua) or !trim($numero) or !trim($bairro) or !trim($abre) or !trim($fecha) or !trim($email) or !trim($telefone)) {
    echo"2 - Tente novamente</b>, faltou preencher corretamente um ou mais campos do formulário.";
}

A more complete form, in this way it checks whether it is null or empty:

function verificaCampos(array $campos){
    foreach($campos as $campo){
        if(empty(trim($campo)) or is_null(trim($campo))){
            return false;
        }
    }
    return true;
}

$nome = 'ok';
$categoria = 'ok';
$rua = 'ok';
$numero = 'ok';
$bairro = 'ok';
$abre = 'ok';
$fecha = 'ok';
$email = 'ok';
$telefone = 'ok';

$campos = [
    $nome, 
    $categoria, 
    $rua, 
    $numero, 
    $bairro, 
    $abre, 
    $fecha, 
    $email, 
    $telefone
];

if (!verificaCampos($campos)) {
    echo"Tente novamente</b>, faltou preencher corretamente um ou mais campos do formulário.";
}
  • I don’t want to compare if the variable is empty, I want to compare if it is equal to a space. I am trying to validate a form and I want fields with only one space not to pass.

  • because you don’t use Trim?

  • I put in another code that won’t let it go.

  • I’ve already used the Tiber and it still passes with only one space. I have the slight impression that Trim does not clean the space, but rather the spaces around the space. But I used and it didn’t work.

  • This second code I posted treats that.

  • I tested with Trim here and it worked.

  • If trim return a string empty, PHP will evaluate as false, so in the first code snippet of the answer, if one or more fields are filled, you will enter the if, which may not make sense (especially when all are filled). I believe in this case the trim should be denied. The last trim is also encompassing the comparison operator.

  • I tested like this and Trim didn’t work, it’s like: <?php $name = "Click Famine"; $neighborhood = "Margarida Garden"; $category = "Açaí"; if(Trim($name) or Trim($neighborhood) or Trim($category) == " ") { echo "Equal"; } Else { echo"Different"; } ?>

  • It was a test, realize it was to fall on Else.

  • 1

    @Andersoncarloswoss thanks for the tip, I made the change, taking advantage, I wanted to thank you, you are the guy who most caught my foot here until today, at first I did not understand right, I thought you had something against me, but it made me grow a lot. :)

  • @dvd test with && and it didn’t work, sometimes I’m misinterpreting, if any one is empty it enters the if.

  • But I don’t want it like this: if they are all the same. I want it like this: if one of them is the same. If one of them is equal to " ", it falls into Isis. That’s how I want it.

  • That’s right, if any one is empty, it enters if, if one or more is empty enter, if all are filled.

  • Tests: http://sandbox.onlinephpfunctions.com/code/7dd411768bca09ecc25ba8799893e53edb4dd29c

Show 9 more comments

4


You are trying to compare all variables at once using only 1 operator ==. That doesn’t work. You would have to compare the variables one by one, this way:

if ($nome == " " or $categoria == " " or $rua == " " or $numero == " " or $bairro == " " or $abre == " " or $fecha == " " or $email == " " or $telefone == " "){
   echo "Tente novamente</b>, faltou preencher corretamente um ou mais campos do formulário.";
}

But you can use another way by creating an array with the variables and then checking one by one with a foreach to see if any of them equals a blank:

$posts = array($nome, $categoria, $rua, $numero, $bairro, $abre, $fecha, $email, $telefone);
$valido = true;

foreach($posts as $item){
    if($item == " ") $valido = false;
}

if(!$valido){
   "Tente novamente</b>, faltou preencher corretamente um ou mais campos do formulário.";
}
  • Man, that’s exactly what I wanted! Just one remark: it only worked without the space between the quotes in my form. It was just that! Thank you! ;-)

  • It must not have worked with the space between the quotes because I am using several functions(Trim, preg_replace and ucwords) before the comparisons. But in normal code it worked perfectly. About what you said, I am new in the forum, but you can leave that from today I will start to point out the correct answer. Again, thank you for understanding the question!

Browser other questions tagged

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