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: 99****1234
> 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 DESCRIPTION PRICE TOTAL
> 4x6 1 4"x6" Standard Print (full-frame from 35mm) .75 $0.75
> 4x6 1 4"x6" Standard Print (full-frame from 35mm) .75 $0.75
> 5x7.5 1 5x7.5 Image:Uploaded Image 3 4.25 $4.25
> 8x12 1 8x12 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]
>
--
Hank Marquardt <[EMAIL PROTECTED]>
http://web.yerpso.net
GPG Id: 2BB5E60C
Fingerprint: D807 61BC FD18 370A AC1D 3EDF 2BF9 8A2D 2BB5 E60C
*** Web Development: PHP, MySQL/PgSQL - Network Admin: Debian/FreeBSD
*** PHP Instructor - Intnl. Webmasters Assn./HTML Writers Guild
*** Beginning PHP -- Starts January 7, 2002
*** See http://www.hwg.org/services/classes
msg41027/pgp00000.pgp
Description: PGP signature

