简体   繁体   中英

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.

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM