Solved

Still not getting correct results

Posted on 2013-01-28
8
140 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
  • 5
  • 3
8 Comments
 
LVL 108

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 28

Author Comment

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

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
 
LVL 108

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

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 108

Expert Comment

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

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 28

Author Closing Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now