0

In the Following Program Preg_Match not aacepting - symbol.

<?php

/*
 * Complete the 'minimumNumber' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts following parameters:
 *  1. INTEGER n
 *  2. STRING password
 */

function minimumNumber($n, $password) {
    // Return the minimum number of characters to make the password strong
    $count = 0;
    // Return the minimum number of characters to make the password strong
    if (!preg_match( '~[A-Z]~', $password)) {
        $count += 1;
    }
    if (!preg_match( '~[a-z]~', $password)) {
        $count += 1;
    }
    if (!preg_match( '~[0-9]~', $password)) {
        $count += 1;
    }
    if (!preg_match( '~[!@#$%^&*()-+]~', $password)) {
        echo 'hit';
        $count += 1;
    }

    if ($n + $count < 6) {
        $count += 6 - ($n + $count);
    }
    
    return $count;

}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$n = intval(trim(fgets(STDIN)));

$password = rtrim(fgets(STDIN), "\r\n");

$answer = minimumNumber($n, $password);

fwrite($fptr, $answer . "\n");

fclose($fptr);

The Program which is checking the Strong Password. The Password should be Minimum 6 chacters and it should contain one number, one small case, one upper case and special character. If anything is missing, have to notify that how many characters needed to make the password strong In that block except those everything is accepting. Am I missing something?

Thanks In Advance

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • I fail to understand the final computation you make, sorry. – arkascha Aug 10 '23 at 14:37
  • This program is to identify how many characters missing to make this password strong – Ganeshkumar Aug 10 '23 at 14:43
  • 1
    `-` is a special character in regex, you need to escape it with a backslash if you're going to match against it specifically. – Alex Howansky Aug 10 '23 at 15:01
  • Like @AlexHowansky already mentioned, the `)-+` part inside the character class denotes a range from ASCII 41 to ASCII 43, which you could easily spot in [tools like regex101](https://regex101.com/r/n73TiB/1). Besides escaping another option is to put the hyphen at [start or end of the character class](https://regex101.com/r/n73TiB/2). – bobble bubble Aug 10 '23 at 16:02
  • Sure, I figured that you implemented to return the number of violations of you rules. But to what avail? What user is meant to understand if you tell him that he violated three of your rules? Users are not meant to think through your ideas, but to use a service of go elsewhere. – arkascha Aug 10 '23 at 17:39

0 Answers0