希望驗證郵政信箱,但想知道是否存在此類驗證。我有地址字段分成地址1和地址2(如PO,公寓,套房信息會去)郵政信箱驗證
例子:
Address 1: 123 Main Street
Address 2: Suite 100
City: Any Town
State: Any State
Zip: Any Zip
郵政信箱(能分BIN爲BOX以及)的例子:
- 郵政信箱123
- PO框123
- PO 123
- 郵政信箱123
- P.O 123
- 箱123
- POB 123
- P.O.B 123
- P.O.B. 123
- 後123
- 郵政信箱123
(我知道有可能更多,我需要驗證的,但是這是我能想到的,隨時補充或更正)
我知道一個正則表達式將是最適合這一點,我已經看到了堆棧#1的其他問題,#2
從另一個問題使用正則表達式,我得到了良好的效果,但它忽略了一些我認爲應該趕上
$arr = array (
'PO Box 123',
'P.O. Box 123',
'PO 123',
'Post Office Box 123',
'P.O 123',
'Box 123',
'#123', // no match
'123', // no match
'POB 123',
'P.O.B 123', // no match
'P.O.B. 123', // no match
'Post 123', // no match
'Post Box 123' // no match
);
foreach($arr as $po) {
if(preg_match("/^\s*((P(OST)?.?\s*O(FF(ICE)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", $po)) {
echo "A match was found: $po\n";
} else {
echo "A match was not found: |$po| \n";
}
}
爲什麼它不捕獲數組中的最後兩個值?
+1在提問前做好研究。 – srijan 2012-10-01 14:40:10
對於一個JS問題,但仍然是一個很好的答案:https://stackoverflow.com/questions/5680050/po-box-regular-expression-validation – turibe 2017-09-22 21:25:35