Forum Moderators: coopster

Message Too Old, No Replies

When Constants Change

Processing Submitted Form Data

         

grandpa

9:38 pm on Feb 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I spent about an hour yesterday tracking down a problem with a script that I "absolutely know" had not been changed, yet it failed to produce any results.

My script 'reads' email messages and takes action based upon certain fields. An example of this would be a line in the email that reads: FDC Authorization Code: 9999999, and my script will parse that string and then do a test for 'FDC Authorization Code'. Fine and dandy, until the format of the message gets changed. Suddenly, results are unexpected and or simply not forthcoming at all. In this example the line in question was changed to 'FDC AuthCode'.

There were at least 4 changes like this made to the email that I process. One of those changes was a show-stopper. Using the same example I changed my test to "FDC Auth" so I could capture both, but this isn't possible with every change. The other thing I did was to convert all of my checks to UPPERCASE. The show-stopper was a case of changing the word Email to EMail. Arrgh!

There must be a better way!

Do you have to deal with this sort of problem or have you got a sure-fire way to minimize problems of this nature?

ironik

12:16 am on Feb 28, 2005 (gmt 0)

10+ Year Member



I'd use a regex in this case, then you can cover all your bases. This function takes input in the form of $text, which can be your plain text or html email and returns an array of matches (so you can take the code directly, or show the line as a match... whatever you want really).

function get_FDC_code($text)
{
$matches = array();
$pattern = "/(FDC)\s?(Auth)(oriz?s?ation)?\s?(code):\s?([0-9]+)/i";
preg_match($pattern, $line, $matches);
return $matches;
}

$resultArray = get_FDC_code($text);

$authCode = $resultArray[5];

// Valid matches:
// FDC AuthCode: 2398348
// FDC Authorization Code: 23492348
// FDCauthcode: 3434

// Uncomment this line if you want to see all matches
// And their array keys
// print_r($resultArray);

ironik

2:13 am on Feb 28, 2005 (gmt 0)

10+ Year Member



What method were you using to validate it? I have a feeling from your other posts here, it's probably more advanced than what I can come up with!

grandpa

6:18 am on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think using regex patterns might work. The idea hadn't occurred to me - I just don't regex much. If can take any mixed case string, or even verify a portion of a string with a regex pattern then I'm set.

What method were you using to validate it?

Not sure I understand the question. Validate what? (Sorry, having a dense moment and trying to cobble a home network together too..)

grandpa

9:49 am on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This might answer your question... I'm reading a file from an array, one line at a time. I've created a series of tests, each to process a single line of the file. Here I'm doing a simple test for a shipping method. Other tests could start a sub-routine, or while loop. The mail I'm processing is from a secure server so there's little risk of someone injecting anything or causing havoc. To that end, my only validation then is to verify the string content with a known value. I added the strtoupper function as a stopgap to their 'meddling' with the email format.

$temp = (substr($val,0,12));
$temp = strtoupper($temp);
if ($temp == "EXPRESS MAIL") {
//
}

ironik

9:13 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



Yeah, a regex will be more flexible than trimming parts of the sub string. You'll be able to match the data more easily that trying to account for every possible change in format.

If you need some regex's just post what data you need to check for and I can try and write the patterns for you.

(I'm using 'check' and 'validate' interchangeably... sorry if I wasn't clear before. I just meant the checks that you using to make sure you were reading the data you were expecting)