Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Still not getting correct results

Posted on 2013-01-28
8
Medium Priority
?
187 Views
Last Modified: 2013-02-16
Can someone please rescue me one more time.

This code by the great Ray Paseur works but according to the users, it is not working correctly.

Here is the current code (not all of it but the meat of it)

function format($matches)
{
    return $matches[1][0].(strlen($matches[2][0])>0?$matches[2][0]:" ").$matches[3][0].(strlen($matches[4][0])>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'], $arr);


// REFORMAT the array
$str = format($arr);

Open in new window


According to the users, few things not working correctly.

1, when you run this code, with say this ID 143154614GG12445

It gives the following output
14 3154614  GG12445

Open in new window


It outs correctly as it puts a single space between 14 and 3154614 and puts 2 spaces before GG. This is fine.

Here is where problem starts.

It outputs several IDs that begin with 14...

According to the users, it should only out the ID you entered which is
14 3154614  GG12445

Open in new window


The other problem is that users should be able to search with zip codes and addresses.

The current app not using the regex we created can work with zip code, address and can output just one ID when searched with that ID.

With the regex integrated, the zip code, addresses don't work. They show an error.

And as stated, it outputs several IDs including the one we searched by.

I tried changing these lines from

preg_match_all($pattern, $_GET['id'], $arr);

to

preg_match_all($pattern, $_GET['id'], $matches);

and from

$str = format($arr);
to

$str = format($matches);

I was able to get it to return just the ID searched with as expected. So, this is good but I still need to ensure that we can still search with address and zip codes.

Please help!!
0
Comment
Question by:sammySeltzer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38830667
For this and any other programming issue, the most important thing you can do is create the test data set.  This article will tell you why that is so important.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

I would also add that I probably did not write line 3 of the code snippet.  I tend to avoid compound statements like that because they are too confusing to debug.

So to go forward with this question, please create a set of test data showing for each test case the following information.  It must be meticulously created - no typographical or spacing errors can be tolerated.  It must be desk-checked for precision.  Once you have that test data set, we can begin addressing the programming that will handle the following information.  The key elements we must see in order to do any programming at all are:

1. The exact input - perhaps several samples
2. For each input, the expected output

Please post your test data set and I'll have a run at showing you the PHP instructions to get from #1 to #2.

Best regards, ~Ray
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 38830925
what is on line 3?
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 38844578
Hello mates, the code below is almost working correctly.

It is formatting ids correctly except the first 2 digits.

For instance, as long as the ID has all digits and no characters or letters, a space needs to be inserted after the first 2 digits. Eg, 32 5414541246

As long as the ID has an N or D after the first digits and the rest are digits, no formatting is required. E.g, 32N5414541246

As long as the ID as digits and GG somewhere in the middle, there should be a space after the first 2 digits and 2 spaces before the GG. E.g, 32 54145  GG1246

Finally, as long as the ID has an N or D after the first 2 digits and GG somewhere between digits, there should only be 2 spaces after GG.

Other important rules,  if the digits are less than 6, then the ID is treated as zipcode.

The code below satisfies most of these requirements except one thing, it is NOT inserting a single space after the first 2 digits.

Does anyone of you php regex gurus know what I am missing?

Here is the code:

$patterns = array(
    '/^(\d\d)(\d{4,})$/',
    '/^(?=.{6,}$)(\d\d[\dDN]\d*)(GG\d*)$/'
); 

$replacements = array( '$1 $2', '$1  $2' );    

$id = preg_replace( $patterns, $replacements, $_GET['id'] );

Open in new window


Thanks a lot in advance
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38844645
I think the thing that is missing is the collection of test data and the corresponding expected outputs!  Once we have that, it will be much easier to help you get the program code to work correctly.

Just curious - where does this data come from?  I've never seen anything quite like this before.
0
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 0 total points
ID: 38844709
Ok, I finally figured this out.

This is a state data that we have to tap into.

So, setting up a test data is awfully difficult.

The only thing I could do is use the $var_dump(...)  to show what data it is collecting.

In any case, this is fixed and working correctly now.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38876931
Sure would be nice if you would post the solution!
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 38876972
This is the solution.

Hopefully, it helps anyone who may bee in similar predicament.

$patterns = array(
    '/^(\d\d)(\d{4,})$/',
    '/^(?=.{6,}$)(\d\d)(\d*)(GG\d*)$/',
    '/^(?=.{6,}$)(\d\d[DN]\d*)(GG\d*)$/'
);

$replacements = array( '$1 $2', '$1 $2  $3', '$1  $2' );

$id = preg_replace( $patterns, $replacements, $_GET['id'] );

Open in new window


works perfectly for us.
0
 
LVL 29

Author Closing Comment

by:sammySeltzer
ID: 38896140
I fixed the problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

604 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