Solved

Still not getting correct results

Posted on 2013-01-28
8
161 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 109

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 109

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 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 109

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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