Check PO Box

Looking at a mailbox check, you need to know if such a check exists. I have the "Address" field, divided into address 1 and address 2 (where such information about PO, Apt, Suite will be displayed)

Example:

Address 1: 123 Main Street Address 2: Suite 100 City: Any Town State: Any State Zip: Any Zip 

Mailbox (can also be used for BIN for BOX) Examples:

  • Mailbox 123
  • PO Box 123
  • PO 123
  • Mailbox 123
  • PO 123
  • Box 123
  • 123

  • 123
  • POB 123
  • POB 123
  • POB 123
  • Message 123
  • Mailbox 123

(I know that I probably need to check more, but this is what I could think of, feel free to add or fix)

I know RegEx is best suited for this, and I saw other questions in Stack # 1 , # 2

Using RegEx from another question, I get good results, but it misses some, I think it should catch

 $arr = array ( 'PO Box 123', 'PO Box 123', 'PO 123', 'Post Office Box 123', 'PO 123', 'Box 123', '#123', // no match '123', // no match 'POB 123', 'POB 123', // no match 'POB 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"; } } 

Why doesn't it catch the last two values ​​in the array?

+8
php regex
source share
3 answers

At the moment, with your regular expression requires an "O" in the "OFFICE". Try ^\s*((P(OST)?.?\s*(O(FF(ICE)?))?.?\s+(B(IN|OX))?)|B(IN|OX)) instead (grouping "O" in conditional match).

EDIT: instead, there should be /^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i . BTW, http://rubular.com/ is a pretty good mechanism for testing regular expressions. It's always nice to know new tools :)

+9
source share

Skip this ...

 / # Beginning of the regex ^ # Beginning of the string \s* # (Any whitespace) (( P # Matches your P (OST)? # Matches your ost .? # Matches the space \s* # (Any whitespace) O # Expects an O - you don't have one. Regex failed. 
+2
source share

This mode works better because it removes unnecessary groups in the match set and simply returns a full match.

Skips message 123:

 /^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)+)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i 

Does not skip message 123:

 /^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)?)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i 

Remove \ d + at the end to skip the quantity request.

+2
source share

All Articles