title: Can you please help modify this code to allow for Address and Zip Code Searches?
body: <p>I am still struggling immensely with these sets of rules below.</p>
  
  <p>Rule: Any search text of 6 or more characters will be interpreted as an ID if one of the following is true:
   all characters are digits, or
   all digits except the third character, which is a D or N, or
   all digits except the third character except the 10th and 11th characters which are GG
   all digits except the third character, which is a D or N, and except the 10th and 11th characters which are GG</p>
  
  <p>IDs will be reformatted as follows:
   If all digits, space inserted after 2nd digit (e.g., 67 00488780011)
   If 3rd character is D or N, no space after 2nd digit (e.g., 14N00339902009)
   If 10th and 11th characters are GG, two spaces before GG (e.g., 14 8725 GG0544)
   If 3rd character is D or N and 10th and 11th characters are GG, no space after 2nd digit but two spaces before GG (e.g., 14N0156 GG0390)</p>
  
  <p>Here is the code I have so far.</p>
  
  <pre><code>function format($matches)
  {
      return $matches[1][0].(strlen($matches[2][0])&gt;0?$matches[2][0]:" ").$matches[3][0].(strlen($matches[4][0])&gt;0?"  ".$matches[4][0]:"");
  }
  
  // CONSTRUCT A REGULAR EXPRESSION
  $pattern
  = '/'         // regex delimiter
  . '('         // START of a capture group
  . '\d{2}'     // exactly two digits
  . ')'         // END of capture group
  . '('         // START SECOND capture group
  . '[\sND]?'     // letters "D" OR "N" in any order or number - This is optional
  . ')'         // END SECOND capture group
  . '('         // START THIRD capture group
  . '\d*'       // any number of digits
  . ')'         // END THIRD capture group
  . '('         // START FOURTH capture group
  . 'GG'        // the letters "GG" EXACTLY
  . '[\d]*'     // any number of digits
  . ')'         // END THIRD capture group
  . '?'         // make the LAST capture group OPTIONAL
  . '/'         // regex delimiter
  ;
  
  // create an array of matches
  preg_match_all($pattern, $_GET['id'], $matches);
  
  // REFORMAT the array
  $id = format($matches);
  </code></pre>
  
  <p>Now, all I would need is to add $id to my query as search filter.</p>
  
  <p>The way this is supposed to work is that you enter an ID into the search box, hit GO. If that ID is valid, the ID is supposed to be reformated according to the rules above and displayed as a link.</p>
  
  <p>The regex I have above does the reformating just fine but there are a few issues I have not been able to resolve.</p>
  
  <p>1, only ID is supposed to be displayed per search if the code is working correctly. However, the search produces several IDs and the one you searched with happens to be one of them.
  2, Users can also search by 5digit zip code and address such as 123 Gregory drive. After reformating with code above, users are no longer able to search with Address or zip code.</p>
  
  <p>Something is definitely wrong with the code but I can't seem to figure it out.</p>
  
  <p>Thanks in advance for being patient with me and for your assistance.</p>
  
  <pre><code>$patterns = array(
      '/^(\d\d)(\d{4,})$/',
      '/^(?=.{6,}$)(\d\d[\dDN]\d*)(GG\d*)$/'
  );
  
  $replacements = array( '$1 $2', '$1  $2' );
  
  $_GET['id'] = $id;
  
  $id = preg_replace( $patterns, $replacements, $_GET['id'] )
  </code></pre>