Re: [PHP] Creating $key, $value pairs
OK, I took a stab at this using your post for input and it should be a good enough starting point for you ... if you do have extraneous ':'s elsewhere in the message then you'll need to be a little more picky with your regex stuff to only pick out what you want -- for example, your post has a couple of sentences that end with ':' before an example ... the script as written picks those up as key value pairs, but since you'll unlikely have those in your order messages I figure you're on safe ground for now -- Of course the really easy way to do this is just to process the form output directly then you have your key/values nice and neat in $HTTP_POST_VARS already:) ... but I'm assuming this is on and processed by a separate server, or with a canned script that only gens emails, so this is what you need to do. Here's the code: #!/usr/bin/php4 -q ? // OK, I assume you use this as a filter, so we're reading from // php://stdin -- $fp = fopen(php://stdin,r); // Initialize a counter for the order items $itemcnt=0; while(!feof($fp)) { $buffer = fgets($fp,8192); // Simple regex effectively 'splitting' on colon(:) with capture if(preg_match('/([^:]+):\t*(.*)/',$buffer,$matches)) { // strip out spaces and /'s in the variable name $matches[1] = preg_replace(/[ \/\]/,,$matches[1]); // create an entry for it in $billinginfo array $billinginfo[$matches[1]] = $matches[2]; } // Regex to match order items (INCOMPLETE!!!) only matches order // number and quantity so far -- Hey I had to leave something for you // to do:) if(preg_match('/([0-9]+x[0-9.]+)\s+([0-9]+).*/',$buffer,$orderitem)) { // since we don't have field ids in the matches, we create our own $order[$itemcnt][Itemnum] = $orderitem[1]; $order[$itemcnt++][Quantity] = $orderitem[2]; } } fclose($fp); // just for testing, dump the arrays -- while(list($key,$val) = each($billinginfo)) { echo $key - $val \n; } echo ORDER HERE!!!\n; for($x=0; $x$itemcnt; $x++) { echo Item: $x \n; while(list($k,$v) = each($order[$x])) { echo $k - $v \n; } echo \n; } ? On Sat, Nov 24, 2001 at 10:20:45PM -0700, Ashley M. Kirchner wrote: I need to convert the following: --- Todays Date: 20-Nov-2001 10:58:24AM Order ID: W25 Customer Email: [EMAIL PROTECTED] Email Promotions: Ok BILL TO: Business Name: Contact Name: Ashley M. Kirchner Day Number: 800 555-1212 Evening Number: 303 555-1212 Fax Number: Address: 3550 Arapahoe Ave #6 Address: Address: City: Boulder State/Province: CO Zip/Postal Code: 80303 Country: USA Payment Method: CC Card Number: 991234 Expiration Date: 01/71 Shipping Method: PICKUP Order Comment: Gift Message: Shipping Instruction: --- ...into $key, $value pairs. Ideally, I'd like to be able to name/use my own names for $key so I can end up with something like: $BillToContact = 'Ashley M. Kirchner' $BillToDayPhone = '800 555-1212' $BillToEvePhone = '303 555-1212' etc. (or maybe with something like: $bill['contact'] = 'Ashley M. Kirchner' $bill['dayphone'] = '800 555-1212' $bill['evephone'] = '303 555-1212' etc.) All of these variables will eventually be shoved into a DB once the entire form has been parsed. And then, at the bottom of that same (submitted) form, there's the actual information on the order, which looks something like this (the spacing is slightly different): (unfortunately, this will wrap rather ugly) --- ORDER NO. QTY UNIT DESCRIPTIONPRICE TOTAL 4x614x6 Standard Print (full-frame from 35mm).75 $0.75 4x614x6 Standard Print (full-frame from 35mm).75 $0.75 5x7.5 15x7.5 Image:Uploaded Image 3 4.25 $4.25 8x12 18x12 Classic Full Frame Image:Uploaded Image 4 10.25 $10.25 SUBTOTAL: $16.00 TAX: $1.18 SHIPPING: $0.00 TOTAL: $17.18 --- This part again I'd like to break up into separate lines, and separate variables for the product no., the quantity, unit, decription, price, total (per line), then the subtotal, tax,
Re: [PHP] Creating $key, $value pairs
OK, probably the easiest thing to do would be to add some kind of state functionality to the loop and use different logic/regex stuff based on where in the process you are ... first test each line for '/^SHIP TO/', '/^BILL TO/' or /^ORDER NO./ ... that could set your 'state' for the script, then you can process each line properly (and it would get around the ':' in an order line problem as you wouldn't execute on that basis ... in my head anyway that would make the main loop into a case construct: switch($stateofscript) { case SHIP: $shippinginfo[$matches[1]] = $matches[2]; break; case BILL: $billinginfo[$matches[1]] = $matches[2]; break; case ORDER: // split your order lines break; default; // must not have hit the SHIPPING INFO yet so do nothing } The other thing you'll need to fix is that you have the generic 'address' tag for 3 different lines in both shipping and billing ... so the logic will need to include a if(isset($shippinginfo[$matches[1]])) thing to prepend or postfix an integer or something onto the index to keep the lines straight. I'd say you're getting there though. Hank On Sun, Nov 25, 2001 at 02:06:52PM -0700, Ashley M. Kirchner wrote: Hank Marquardt wrote: Actually you're OK here (I think ...) if a few of them end up blank, that's OK, just ignore them, you'll know the ones you want based on the real email you receive ... as for the folks putting To: someone, From: someone ... I wrote the regex to accomodate that ... it takes anything up to the first ':' and claims that as the variable name, then discards the ':' after and finally grabs the rest of the line (.*) as the remainder ... that can include ':' without problem ... in fact if you look at your example, it handles the first 'timestamp' field just fine:) Yeap, I noticed that as soon as I ran the first (full) email through it. And of course, there're some problems. The incoming email has two sections in it which will contain the same variables. What I posted only contained a 'BILL TO:' section. The full email also contains a 'SHIP TO:' section. But because the script creates variables based on the fields in the email, it overwrites the first section: SHIP TO: Business Name: Contact Name: PhotoCraft Day Number: 303.442.6410 Evening Number: Fax Number: Address: 3550 Arapahoe Ave Address: Suite 6 Address: City: Boulder State/Province: CO Zip/Postal Code: 80303 Country: USA BILL TO: Business Name: Contact Name: Ashley M. Kirchner Day Number: 800 555-1212 Evening Number: 303 555-1212 Fax Number: Address: 3550 Arapahoe Ave #6 Address: Address: City: Boulder State/Province: CO Zip/Postal Code: 80303 Country: USA This is part of why I asked whether it was possible to use my own variable naming, so I could end up with one of: $ShipToContact$BillToContact $ShipToDayPhone $BillToDayPhone $ShipToEvePhone $BillToEvePhone or: $ShipTo['contact']$BillTo['contact'] $ShipTo['DayPhone'] $BillTo['dayphone'] $ShipTo['EvePhone'] $BillTo['evephone'] And then there's still the order items themselves. Right now, the script is cutting the lines because of the : matching. So, a line like this (wrapping alert): ORDER NO. QTY UNIT DESCRIPTION PRICE TOTAL 4x6 1 4x6 Standard Print (full-frame from 35mm) Image:Uploaded Image 1 .75$0.75 ...will end up: $key: 4x614x6StandardPrint(full-framefrom35mm)Image $val: 'Uploaded Image 1 .75 $0.75' ORDER HERE: Item: 0 Itemnum - 4x6 Quantity - 1 Not quite what it should be. I think I'm going to limit where it should do the $key - $value matching, and figure something else out for the rest of the message. the whole thing would be a lot easier in the last one if they actually tab delimit the fields, then a simple split() call would handle it nicely but what you posted was spaces. Yeah, the original incoming email is a mess really. No tab delimited anything, and some of the fields have extraneous spaces after each value, some just have a row of spaces (if the value's blank), some 80 characters long. -- H | Life is the art of drawing without an eraser. - John Gardner + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 Director of Internet Operations / SysAdmin. 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave, #6 http://www.pcraft.com . . .. Boulder, CO
[PHP] Creating $key, $value pairs
I need to convert the following: --- Todays Date: 20-Nov-2001 10:58:24AM Order ID: W25 Customer Email: [EMAIL PROTECTED] Email Promotions: Ok BILL TO: Business Name: Contact Name: Ashley M. Kirchner Day Number: 800 555-1212 Evening Number: 303 555-1212 Fax Number: Address: 3550 Arapahoe Ave #6 Address: Address: City: Boulder State/Province: CO Zip/Postal Code: 80303 Country: USA Payment Method: CC Card Number: 991234 Expiration Date: 01/71 Shipping Method: PICKUP Order Comment: Gift Message: Shipping Instruction: --- ...into $key, $value pairs. Ideally, I'd like to be able to name/use my own names for $key so I can end up with something like: $BillToContact = 'Ashley M. Kirchner' $BillToDayPhone = '800 555-1212' $BillToEvePhone = '303 555-1212' etc. (or maybe with something like: $bill['contact'] = 'Ashley M. Kirchner' $bill['dayphone'] = '800 555-1212' $bill['evephone'] = '303 555-1212' etc.) All of these variables will eventually be shoved into a DB once the entire form has been parsed. And then, at the bottom of that same (submitted) form, there's the actual information on the order, which looks something like this (the spacing is slightly different): (unfortunately, this will wrap rather ugly) --- ORDER NO. QTY UNIT DESCRIPTIONPRICE TOTAL 4x614x6 Standard Print (full-frame from 35mm).75 $0.75 4x614x6 Standard Print (full-frame from 35mm).75 $0.75 5x7.5 15x7.5 Image:Uploaded Image 3 4.25 $4.25 8x12 18x12 Classic Full Frame Image:Uploaded Image 4 10.25 $10.25 SUBTOTAL: $16.00 TAX: $1.18 SHIPPING: $0.00 TOTAL: $17.18 --- This part again I'd like to break up into separate lines, and separate variables for the product no., the quantity, unit, decription, price, total (per line), then the subtotal, tax, shipping cost and final (grant) total. Again, if I can somehow use my own variable names, that'd be great. ($product['no'] $product['quantity'] $product['unit'] etc. $order['subtotal'] $order['tax'] $order['shipping'] $order['total'] etc.) I don't know yet how to create different entries for each line in that order - kinda stupid to create $product_1['no'], $product_2['no'], $product_3['no'], $product_4['no'], but then again, I don't know. -- H | Life is the art of drawing without an eraser. - John Gardner + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 Director of Internet Operations / SysAdmin. 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave, #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]