Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular Expression to Capture PO Box using PHP

Posted on 2004-10-21
5
Medium Priority
?
595 Views
Last Modified: 2008-03-06
I need to determine if an address text input field contains a PO BOX or any derivative of PO BOX - PO, BOX, P.O. BOX, etc. I have found many good solutions on this site using JavaScript, however, I really must do this with PHP. I have been experimenting with preg_grep(), as it appears this is the best function. However, my regular expressions are sorely lacking and it has become very frustrating and I've spent a great deal of time trying to figure this out. I am inserting some code I have been working with to try and cover all the PO Box possiblities. I simply can't get it to work.

$result=preg_grep("/((P\.?O\.?(B\.?)?(\s+Box)?)|(Post\s+Office(\s+Box)?))\s*#?[0-9]+/", $addr, $regs);

This is a derivative of one of the solutions I found in the JavaScript area.

I am a new member and this is my very first post. It was my intention follow etiquette. Please let me know if I have stepped out of bounds.

Thanks in advance.
0
Comment
Question by:mwheeler_fsd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12374317
preg_grep is intended for arrays, not strings.  Use preg_match instead.

I haven't picked apart the regexp, but it looks like it might work.
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12374359
You'll probably want to make it case insensitive.  Add the letter "i" between the last / and closing "
...#?[0-9]+/i", $addr, $regs);
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 2000 total points
ID: 12374408
<?php
$addr = array("PO 123", "PO BOX 123", "P.O. BOX 123", "BOX 123");
print_r(preg_grep("/((P\.?O\.?(B\.?)?(\s+Box)?)|(Post\s+Office(\s+Box)?))\s*#?[0-9]+/i", $addr));
?>

matches:
Array
(
    [0] => PO 123
    [1] => PO BOX 123
    [2] => P.O. BOX 123
)
0
 

Author Comment

by:mwheeler_fsd
ID: 12374473
Thank you snoyes_jw - one quick question: You had commented that preg_match() might be more appropriate the preg_grep() because the address I am checking is actually just a string - is one better then the other?
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12374724
preg_match is designed to check strings, so that's what you should use.  In the example, I was checking an array to show that it worked on several different strings, so I used preg_grep.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question