Even if you can do exactly the same with arrays and array_intersect
, this is the opportunity to use the ds module and the Set class:
$cities = new \Ds\Set([
'birmingham', 'bradford', 'brighton and hove', 'bristol', 'cambridge',
'canterbury', 'carlisle', 'chelmsford', 'chester', 'chichester', 'colchester',
'coventry', 'derby', 'doncaster', 'durham', 'ely', 'exeter', 'gloucester',
'hereford', 'kingston upon hull', 'lancaster', 'leeds', 'leicester',
'lichfield', 'lincoln', 'liverpool', 'london', 'city of london', 'manchester',
'milton keynes', 'newcastle upon tyne', 'norwich', 'nottingham', 'oxford',
'peterborough', 'plymouth', 'portsmouth', 'preston', 'ripon', 'salford',
'salisbury', 'sheffield', 'southampton', 'southend-on-sea', 'st albans',
'stoke on trent', 'stoke-on-trent', 'sunderland', 'truro', 'wakefield',
'wells', 'westminster', 'city of westminster', 'winchester', 'wolverhampton',
'worcester', 'york', 'aberdeen', 'dundee', 'dunfermline', 'edinburgh',
'glasgow', 'inverness', 'perth', 'stirling', 'bangor', 'cardiff', 'newport',
'st asaph', 'st davids', 'swansea', 'wrexham', 'armagh', 'belfast', 'lisburn',
'derry', 'newry']);
$location = 'Lawrence Street, York, YO10 3EU, United Kingdom';
$locaSet = new \Ds\Set(preg_split('~\s*,\s*~', strtolower($location)));
$inter = $cities->intersect($locaSet);
if (!$inter->isEmpty()) {
echo $inter->get(0); // york
}