Dravenzo
Dravenzo

Reputation: 129

How to check for a specific letter or number in array

I am creating a code where the there is an array with multiple different variables and the function checks if the variable has correct type of ID and it will echo if it is correct or not. Basic rules would be:

  1. The Delimiter can only have '-', '/', '.' symbols.
  2. The Prefix can only have two uppercase letters (no symbols or numbers)
  3. The Suffix can only have 4numbers (no symbols or letters)

I have so far managed to code it so that the function checks if those three specific symbols exist in each ID, but I can't seem to figure out how to get the function to also check for prefix and suffix so that any ID with wrong prefix or suffix would come out as not valid ID.

My code:

            function checkSpecialChar () {
                $id = array('AA-2365','AA/2468','AA.2177','AA:2365','A1.2365','AA-2O65, AA-4365');
                /* $specialChar = explode(" ", $id); */
                foreach ($id as $check) {
                    if (strpbrk($check,'-')) {
                        echo 'The id is valid';
                        echo '<br>';
                    } else if (strpbrk($check, '/')) {
                        echo 'The id is valid';
                        echo '<br>';
                    } else if (strpbrk($check, '.')) {
                        echo 'The id is valid';
                        echo '<br>';
                    } else {
                        echo'The id is not valid';
                        echo '<br>';
                    };
                };
            };

            checkSpecialChar();

I wanted to use explode() to split each ID so that I can then check prefix and suffix as well but from my understanding explode only works on strings not arrays.

Upvotes: 0

Views: 76

Answers (1)

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2115

Using a regular expression:

function checkSpecialChar(string $id): bool {
    return preg_match('~^[A-Z][A-Z][-/.]\d{4}$~', $id);
}

print_r(
    array_map(
        function($id) { return [$id, checkSpecialChar($id) ? 'T' : 'F']; },
        ['AA-2365','AA/2468','AA.2177','AA:2365','A1.2365','AA-2O65, AA-4365']
    )
);

Output:

Array
(
    [0] => Array
        (
            [0] => AA-2365
            [1] => T
        )

    [1] => Array
        (
            [0] => AA/2468
            [1] => T
        )

    [2] => Array
        (
            [0] => AA.2177
            [1] => T
        )

    [3] => Array
        (
            [0] => AA:2365
            [1] => F
        )

    [4] => Array
        (
            [0] => A1.2365
            [1] => F
        )

    [5] => Array
        (
            [0] => AA-2O65, AA-4365
            [1] => F
        )

)

If you don't want to use regular expressions:

function checkSpecialChar(string $id): bool {
    return
        ctype_upper(substr($id, 0, 2))
        && in_array(substr($id, 2, 1), ['-', '/', '.'])
        && ctype_digit(substr($id, 3));
}

Upvotes: 1

Related Questions