Jump to content

Recommended Posts

Dear experts,

 

I really, really need your expert assistance.

 

we have an id that when entered into a search box, displays the same id you searched with, if that id is valid and it exists.

 

On the same search box, you can search with a zip code or an address.

 

Issue we have with the id is that it has to follow a certain format for it to produce search results.

 

For instance, if the id has all digits, then a space is to be inserted after the first 2 digits.

 

If has an N or D after the first 2 digits and no additional digits or characters, then no formatting is needed.

 

If it has mostly digits and GG in between the digits, then a space after the first 2 digits as described above and 2 spaces before GG need.

 

If there is N or D after first 2 digits and GG in between the digits, then only 2 spaces before the GG (35D12543 GG125)

 

If the user is searching with zipcode, then we need to recognize that it is zipcode.

 

In other words, if all digits and the digits are less than 6 digits, then it is zip code, no formatting.

 

Now, this is where the real trick comes in. If user is searching by address, how do I modify the code to instruct it NOT to format the address so user can get results?

 

Here is the code that I have so far.

 

The code seems to address the issue of address and zip code but isn't reformatting the ID correctly so that

  1. it is reformating ID if ID has ALL digits like 35 1254311235 but it is not reformatting ID if ID has digits with GG in between like 3512543 GG125. There should be a space after 2 digits and 2 spaces before GG
     
  2. the regex for some reason assumes that the GG is the 10th and 11th character and therefore reformats correctly if ID has GG as 10th and 11th character. If the characters don't fall within the 10th and 11th, it doesn't work.

Can you please, please take a look at my regex and see what needs fixing?

 

Please forgive me for the long text and thank you very much in advance for your patience and assistance.

 

$patterns = array(
'/^(\d\d)(\d{4,})$/',
'/^(\d\d[\dDN]\d{6})(GG\d*)$/'
);
$replacements = array( '$1 $2', '$1 $2' );
$_GET['id'] = $id;	
$id = preg_replace( $patterns, $replacements, $_GET['id'] );

Link to comment
https://forums.phpfreaks.com/topic/273913-i-am-having-problem-formatting-id/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.