fork(5) download
  1. <?php
  2.  
  3. // your code goes here
  4.  
  5.  
  6.  
  7. $addr1 = "5285 KEYES DR KALAMAZOO MI 49004 2613";
  8. $addr2 = "PO BOX 35 COLFAX LA 71417 35";
  9. $addr3 = "64938 MAGNOLIA LN APT B PINEVILLE LA 71360-9781";
  10.  
  11. function extract_zipcode($address, $remove_statecode = false) {
  12. $zipcode = preg_match("/\b[A-Z]{2}\s+\d{5}(-\d{4})?\b/", $address, $matches);
  13. return $remove_statecode ? preg_replace("/[^\d\-]/", "", extract_zipcode($matches[0])) : $matches[0];
  14. }
  15.  
  16. echo extract_zipcode($addr1, 1) . ' ';
  17. echo extract_zipcode($addr2) . ' ';
  18.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
49004 LA 71417