WC_Validation::is_GB_postcode PHP Method

is_GB_postcode() public static method

Check if is a GB postcode.
Author: John Gardner
public static is_GB_postcode ( string $to_check ) : boolean
$to_check string A postcode
return boolean
    public static function is_GB_postcode($to_check)
    {
        // Permitted letters depend upon their position in the postcode.
        // https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
        $alpha1 = "[abcdefghijklmnoprstuwyz]";
        // Character 1
        $alpha2 = "[abcdefghklmnopqrstuvwxy]";
        // Character 2
        $alpha3 = "[abcdefghjkpstuw]";
        // Character 3 == ABCDEFGHJKPSTUW
        $alpha4 = "[abehmnprvwxy]";
        // Character 4 == ABEHMNPRVWXY
        $alpha5 = "[abdefghjlnpqrstuwxyz]";
        // Character 5 != CIKMOV
        $pcexp = array();
        // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
        $pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([0-9]{1}' . $alpha5 . '{2})$/';
        // Expression for postcodes: ANA NAA
        $pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/';
        // Expression for postcodes: AANA NAA
        $pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/';
        // Exception for the special postcode GIR 0AA
        $pcexp[3] = '/^(gir)(0aa)$/';
        // Standard BFPO numbers
        $pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';
        // c/o BFPO numbers
        $pcexp[5] = '/^(bfpo)(c\\/o[0-9]{1,3})$/';
        // Load up the string to check, converting into lowercase and removing spaces
        $postcode = strtolower($to_check);
        $postcode = str_replace(' ', '', $postcode);
        // Assume we are not going to find a valid postcode
        $valid = false;
        // Check the string against the six types of postcodes
        foreach ($pcexp as $regexp) {
            if (preg_match($regexp, $postcode, $matches)) {
                // Remember that we have found that the code is valid and break from loop
                $valid = true;
                break;
            }
        }
        return $valid;
    }

Usage Example

コード例 #1
0
ファイル: validation.php プロジェクト: tlovett1/woocommerce
 /**
  * Data provider for test_is_GB_postcode.
  *
  * @since 2.4
  */
 public function data_provider_test_is_GB_postcode()
 {
     return array(array(true, WC_Validation::is_GB_postcode('AA9A 9AA')), array(true, WC_Validation::is_GB_postcode('A9A 9AA')), array(true, WC_Validation::is_GB_postcode('A9 9AA')), array(true, WC_Validation::is_GB_postcode('A99 9AA')), array(true, WC_Validation::is_GB_postcode('AA99 9AA')), array(true, WC_Validation::is_GB_postcode('BFPO 801')), array(false, WC_Validation::is_GB_postcode('99999')), array(false, WC_Validation::is_GB_postcode('9999 999')), array(false, WC_Validation::is_GB_postcode('999 999')), array(false, WC_Validation::is_GB_postcode('99 999')), array(false, WC_Validation::is_GB_postcode('9A A9A')));
 }