php extract UK postal code and validate it

前端 未结 3 1641
误落风尘
误落风尘 2021-02-11 08:09

I have some text blocks like

John+and+Co-Accountants-Hove-BN31GE-2959519

I need a function to extract the postcode \"BN31GE\". It may happen to not exist

3条回答
  •  一个人的身影
    2021-02-11 08:43

    Find below code to extract valid UK postal code. It return array if post code found otherwise empty.

    0) {
        foreach($getArray as $key=>$val) {
            if(preg_match("/^(([A-PR-UW-Z]{1}[A-IK-Y]?)([0-9]?[A-HJKS-UW]?[ABEHMNPRVWXY]?|[0-9]?[0-9]?))\s?([0-9]{1}[ABD-HJLNP-UW-Z]{2})$/i",strtoupper($val),$postcode)) {
                $getPostcode = $postcode[0];
            }
        }
    }
    print"
    ";
    print_r($getPostcode); 
    ?>
    

提交回复
热议问题