PHP Comparison between fields

Asked

Viewed 62 times

1

Good,

I’m making 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" just posts the $postcomment field and posts above 20 characters, it passes,

Or

If you post the $uploaded field, and this field starts with http://, you pass.

How do I compare?

2 answers

1

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

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://"
}

0

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

For example:

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

To know if it begins with http you can use REGEX, just remember that Urls can, start with the http as to https, so you can use:

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

Soon:

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

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

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

    $passa = true;

}

Test it out here.

The use of xor (^) will cause only if the $uploaded or the $postcomment are completed, but not both, in which case if both are completed "will not continue".

Browser other questions tagged

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