I was wondering how to go about finding a postcode in an address string and making it its own variable using regex in .
for example
$address = '123 My Street, My Area, My City, AA11 1AA'
I want $postcode = 'AA11 1AA'
I also want to remove the postcode that's found from the address string.
I have this so far.
$address = strtolower(preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $data[2]));
$postcode = 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",$address,$post);
$postcode = $post;