Regular Expression to Capture PO Box using PHP

Posted on 2004-10-21
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.
Question by:mwheeler_fsd
    LVL 33

    Expert Comment

    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.
    LVL 33

    Expert Comment

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

    Accepted Solution

    $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));

        [0] => PO 123
        [1] => PO BOX 123
        [2] => P.O. BOX 123

    Author Comment

    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?
    LVL 33

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now