SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Best way to trim zip codes

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to trim zip codes

    I've got some hyphenated zip codes as values in an array and I need to dump the hyphen (and everything after the hyphen). Is trim() the best function for this?

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably not the most efficient, but seems to do the job:
    PHP Code:
    <?php
    $a 
    = array('1234-456''9874-412');
    function 
    strip_hyphen($s){
        
    $bits explode('-'$s);
        return 
    $bits[0];
    }
    $new array_map('strip_hyphen'$a);
    print_r($new);
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $new_zip = substr( $old_zip, 0, 5 );

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •