我想知道如何在地址字符串中找到邮政编码并使用正则表达式使其成为自己的变量。
例如
$address = '123 My Street, My Area, My City, AA11 1AA'
我想要 $postcode = 'AA11 1AA'
我还想删除从地址字符串中找到的邮政编码。
到目前为止我有这个。
$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;
最佳答案
这对我有用:
$value = "Big Ben, Westminster, London, SW1A 0AA, UK";
$pattern = "/((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))/i";
preg_match($pattern, $value, $matches);
$value = $matches[0]; // Will give you SW1A 0AA
http://www.sitepoint.com/community/t/extract-uk-postcode-from-string/31962
关于php - 从地址字符串中提取邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18234634/