PHP Field Comparison

1

Good,

I'm doing a system of comments, but I'm having problems, How do I check between two fields.

$postcomment = $_POST['message'];
$uploaded = $_POST['upload'];

For example, if the "user" only posts the $ postcomment field and if you post more than 20 characters,

Or

If you post the $ uploaded field, and if that field starts with http: //, it passes.

How do I make this comparison?

    
asked by anonymous 02.04.2017 / 23:46

2 answers

1

You can use if's for verification, here's an example:

if (strlen($postcomment) >= 20 && empty($uploaded)) {
    // Veririca se o campo $postcomment tem mais de 20 caracteres e se o campo $uploaded esta vazio
} else if (!empty($uploaded) && preg_match('/^http:\/\//', $uploaded)) {
  // Verifica se o campo $uploaded não esta vazio e se começa com "http://"
}
    
02.04.2017 / 23:56
0

Use mb_strlen to know how many characters you have and compare with the minimum number of characters required.

For example:

if(mb_strlen($postcomment) > MINIMO_DE_CARACTERES && !isset($uploaded)){
     $passa = true;
}

To find out if you start with http you can use REGEX, just remember that URLs can, start with http and https , so you can use:

/^(http|https):\/\//

Logo:

if(preg_match('/^(http|https):\/\//', $uploaded) && !isset($postcomment)){
     $passa = true;
}

You do not say what should happen if both are filled, this is a situation that should be dealt with, this would be a solution:

if((!empty($postcomment) ^ !empty($uploaded))
   && (mb_strlen($postcomment) > 20 || preg_match('/^(http|https):\/\//', $uploaded))){

    $passa = true;

}

Try this here.

The use of xor ( ^ ) will only cause $uploaded or $postcomment to be filled, but not both, in this case if both are filled "will not continue".

    
03.04.2017 / 00:58