<?php
$userInput = 'YO264UU';
// remove all spaces and upper case
$userInput = strtoupper(preg_replace('/\s+/', '', $userInput));
if (true || isValidPostcode($userInput)) {
// Basic postcode formatting - add a space before the last 3 characters
$formatted = substr($userInput, 0, -3) . ' ' . substr($userInput, -3);
// now look up in db using $formatted
} else {
// invalid postcode!
}
var_dump($formatted);