Php Regex&;菲律宾元邮政
全部。检索传入到已设置脚本的某些POST值时出现问题。POST密钥不能更改,因为它们来自PayPal的请求。但是,我需要一些方法来动态获取这些值 当一个人购买多个项目时,我会返回POST值,例如:Php Regex&;菲律宾元邮政,php,regex,post,Php,Regex,Post,全部。检索传入到已设置脚本的某些POST值时出现问题。POST密钥不能更改,因为它们来自PayPal的请求。但是,我需要一些方法来动态获取这些值 当一个人购买多个项目时,我会返回POST值,例如: item_name=foo item_name1=bar item_name2=blah 我在想,我必须用某种正则表达式来获取值,但我不确定语法,或者甚至不确定是否可以在超全局上使用正则表达式 可能是这样的: $exp = "/item_name(d+)/"; foreach($_POST as $
item_name=foo
item_name1=bar
item_name2=blah
我在想,我必须用某种正则表达式来获取值,但我不确定语法,或者甚至不确定是否可以在超全局上使用正则表达式
可能是这样的:
$exp = "/item_name(d+)/";
foreach($_POST as $key => $val)
{
preg_match($exp,$key,$match);
}
正如你所知道的,我对正则表达式非常不好。任何帮助都将不胜感激。:)
--编辑--
好的,我需要抓住两个主要的发帖键。问题是,键名称后可能有任何数字(正整数):
item_name
item_name1
item_name2
item_number
item_number1
item_number2
但根据购买的产品数量,最终数字可以一直到10。
我需要做的是获取其中每个项目的值,并将每个项目的编号添加到一个字符串中,以插入到数据库中的事务表中。
也许更接近:
$itms = '';
foreach($_POST as $key => $val)
{
if (strpos($key, 'item_name') === 0)
{
if($itms = '')
{
$itms = $val;
}
else
{
$itms .= ','.$val;
}
}
// don't really want to do it like this because i'd have to do it 10 times
if (strpos($key, 'item_name1') === 0)
{
if($itms = '')
{
$itms = $val;
}
else
{
$itms .= ','.$val;
}
}
}
// insert into DB after collecting all values
是的,这是来自IPN
--编辑2--
这是我的IPN侦听器脚本的内容:
case 'ipnHandle' :
$header = '';
$retArray = array();
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
if (!$fp)
{
$err_msg = 'Could not open the Paypal site to Verify Transaction';
}
else
{
fputs ($fp, $header . $req);
while (!feof($fp))
{
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0)
{
if($_POST['receiver_email'] == "chaoskreator@gmail.com")
{
if($_POST['payment_status'] == 'Completed')
{
$sql = 'SELECT transaction_id FROM '.SHOP_TRANS_TABLE.' WHERE transaction_pid = '.$_POST['txn_id'];
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
if($row != 0 && $row != null)
{
$id_exp = '/^item_number(\d*)$/';
$qty_exp = '/^quantity(\d*)$/';
$price_exp = '/^mc_gross(\d*)$/';
$items = array();
$qtys = array();
$prices = array();
foreach($_POST as $key => $val)
{
$match = Array();
if(preg_match($id_exp, $key, $match))
{
$items[] = $val;
}
if(preg_match($qty_exp, $key, $match))
{
$qtys[] = $val;
}
if(preg_match($price_exp, $key, $match))
{
$prices[] = $val;
}
}
}
}
}
$itmStr = implode(",", $items);
$qtyStr = implode(",", $qtys);
$priceStr = implode(",", $prices);
$data = '';
$file = "verifyLog.txt";
$fh = fopen($file, "a+");
foreach($_POST as $key => $value)
{
if($data == '')
{
$data = '['.$key.']'.$value;
}
else
{
$data .= ',['.$key.']'.$value;
}
}
$data .= "\r\n".$itmStr."\r\n".$qtyStr."\r\n".$priceStr;
fwrite($fh, $data);
fclose($fh);
}
else if (strcmp ($res, "INVALID") == 0)
{
$data = '';
$file = "failLog.txt";
$fh = fopen($file, "a+");
foreach($_POST as $value)
{
if($data == '')
{
$data = '['.$key.']'.$value;
}
else
{
$data .= ',['.$key.']'.$value;
}
}
fwrite($fh, $data);
fclose($fh);
}
}
fclose ($fp);
}
break;
这是记录到verifyLog.txt的响应,末尾没有逗号分隔的字符串…:
[test_ipn]1,[payment_type]instant,[payment_date]20:47:04 Apr 19, 2011 PDT,[payment_status]Completed,[payer_status]verified,[first_name]John,[last_name]Smith,[payer_email]buyer@paypalsandbox.com,[payer_id]TESTBUYERID01,[business]seller@paypalsandbox.com,[receiver_email]seller@paypalsandbox.com,[receiver_id]TESTSELLERID1,[residence_country]US,[item_name]something,[item_number]AK-1234,[item_name1]somethingElse,[item_number1]1234346dfg,[quantity]1,[quantity1]1,[shipping]8.50,[tax]2.02,[mc_currency]USD,[mc_fee]0.44,[mc_gross]15.34,[mc_gross_1]12.34,[mc_handling]2.06,[mc_handling1]1.67,[mc_shipping]3.02,[mc_shipping1]1.02,[txn_type]cart,[txn_id]4420347,[notify_version]2.4,[custom]xyz123,[invoice]abc1234,[charset]windows-1252,[verify_sign]XXXXXXXXXXXX
这让我喝多了。lol如果只是匹配一个前缀,那么正则表达式可以说是矫枉过正。下面这样一个简单的字符串比较将允许您识别项目
如果您只是匹配一个前缀,那么正则表达式可以说是矫枉过正。下面这样一个简单的字符串比较将允许您识别项目
下面的代码将检查每一个,看它是否是其中一个项目键,如果是,则抓取ID。您的示例显示了一个上面没有数字的,由于我不熟悉您正在处理的Paypal内容,所以在进行匹配时,我继续将ID设置为0
<?php
$exp = '/^item_name(\d*)$/';
$values = Array();
foreach( $_POST as $key => $val )
{
$match = Array();
//If this is one of the item name variables
if( preg_match( $exp, $key, $match ) )
{
//then put it into the array of captured values
$values[] = $val;
}
}
$commaSeparatedForDb = implode( ',', $values );
下面的代码将检查每一个项目键,看它是否是其中一个项目键,如果是,则抓取ID。您的示例显示了一个上面没有数字的,由于我不熟悉您正在处理的Paypal内容,所以在进行匹配时,我继续将ID设置为0
<?php
$exp = '/^item_name(\d*)$/';
$values = Array();
foreach( $_POST as $key => $val )
{
$match = Array();
//If this is one of the item name variables
if( preg_match( $exp, $key, $match ) )
{
//then put it into the array of captured values
$values[] = $val;
}
}
$commaSeparatedForDb = implode( ',', $values );
试试这个
$i = 0;
do {
$key = 'item_name';
if ($i > 0) {
$key .= $i; // append number to key if > 0
}
if (isset($_POST[$key])) {
echo "found $key => ".$_POST[$key];
// do something with it
}
$i++;
} while (isset($_POST[$key]));
基于以上编辑的问题
if($_POST['payment_status'] == 'Completed')
{
$sql = 'SELECT transaction_id FROM '.SHOP_TRANS_TABLE.' WHERE transaction_pid = '.$_POST['txn_id'];
$result = $db->sql_query($sql);
// where is sql error checking, what if this sql has failed??
$row = $db->sql_fetchrow($result);
if($row != 0 && $row != null) {
// have you checked to see if the code is ever reaching this point?
// is it possible your database row doesn't exist
// this part here looks fine for extracting $_POST values
$id_exp = '/^item_number(\d*)$/';
$qty_exp = '/^quantity(\d*)$/';
$price_exp = '/^mc_gross(\d*)$/';
// maybe we should declare these arrays earlier.
// if this test fails, "if($row != 0 && $row != null) "
// you are still imploding the arrays below even though they haven't been declared
$items = array();
$qtys = array();
$prices = array();
foreach($_POST as $key => $val) {
// no need to store the matches as you aren't using them anyway
// $match = Array();
// changed 3x if statements to elseif
// this is not essential and won't effect the outcomes
// but is good practice as there is no point testing all 3 cases if first/second case already matches
if(preg_match($id_exp, $key)) {
$items[] = $val;
} elseif(preg_match($qty_exp, $key)) {
$qtys[] = $val;
} elseif (preg_match($price_exp, $key)) {
$prices[] = $val;
}
}
}
$itmStr = implode(",", $items);
$qtyStr = implode(",", $qtys);
$priceStr = implode(",", $prices);
试试这个
$i = 0;
do {
$key = 'item_name';
if ($i > 0) {
$key .= $i; // append number to key if > 0
}
if (isset($_POST[$key])) {
echo "found $key => ".$_POST[$key];
// do something with it
}
$i++;
} while (isset($_POST[$key]));
基于以上编辑的问题
if($_POST['payment_status'] == 'Completed')
{
$sql = 'SELECT transaction_id FROM '.SHOP_TRANS_TABLE.' WHERE transaction_pid = '.$_POST['txn_id'];
$result = $db->sql_query($sql);
// where is sql error checking, what if this sql has failed??
$row = $db->sql_fetchrow($result);
if($row != 0 && $row != null) {
// have you checked to see if the code is ever reaching this point?
// is it possible your database row doesn't exist
// this part here looks fine for extracting $_POST values
$id_exp = '/^item_number(\d*)$/';
$qty_exp = '/^quantity(\d*)$/';
$price_exp = '/^mc_gross(\d*)$/';
// maybe we should declare these arrays earlier.
// if this test fails, "if($row != 0 && $row != null) "
// you are still imploding the arrays below even though they haven't been declared
$items = array();
$qtys = array();
$prices = array();
foreach($_POST as $key => $val) {
// no need to store the matches as you aren't using them anyway
// $match = Array();
// changed 3x if statements to elseif
// this is not essential and won't effect the outcomes
// but is good practice as there is no point testing all 3 cases if first/second case already matches
if(preg_match($id_exp, $key)) {
$items[] = $val;
} elseif(preg_match($qty_exp, $key)) {
$qtys[] = $val;
} elseif (preg_match($price_exp, $key)) {
$prices[] = $val;
}
}
}
$itmStr = implode(",", $items);
$qtyStr = implode(",", $qtys);
$priceStr = implode(",", $prices);
自己发现阵列。顺便说一句,它应该是\d
,而不是d
。和*
,而不是+
您想匹配什么?您需要了解数组是什么,以及foreach做什么。您的代码编辑离答案越来越远,而完全有效的答案已经发布。请自己发现数组。顺便说一句,它应该是\d
,而不是d
。和*
,而不是+
您想匹配什么?您需要了解数组是什么,以及foreach做什么。您的代码编辑离答案越来越远,而完全有效的答案已经发布。这更有意义,但我如何处理每个实例呢?如果(strpos($key,'item_name1')==0){if(strpos($key,'item_name1')==0){///如果你有第二个产品}我就这么做。}
也许我误解了,但你不必这么做。由于您正在查看整个$\u POST
数组,$key
和$value
具有所需的一切。无需硬编码。对我发布的逻辑中的每个项目执行您需要执行的操作。抱歉,在先前发布时意外地按了enter键。让我返回并编辑原始帖子以显示更多细节。我猜这是来自IPN,您只需执行一个foreach()foreach($\u Post as$key=>$value),这更有意义,但我如何处理每个实例呢?如果(strpos($key,'item_name1')==0){if(strpos($key,'item_name1')==0){///如果你有第二个产品}我就这么做。}
也许我误解了,但你不必这么做。由于您正在查看整个$\u POST
数组,$key
和$value
具有所需的一切。无需硬编码。对我发布的逻辑中的每个项目执行您需要执行的操作。抱歉,在先前发布时意外地按了enter键。让我回去编辑原始帖子以显示更多细节。我猜这是来自IPN,你只需要做一个foreach()foreach($\u Post as$key=>$value)我以前确实尝试过类似的方法,但失败得很惨。也许我可以根据自己的需要来调整。我试试看。这行有一个输入错误,对不起,echo“found$key=>”$\u POST[$key];将$\u POST['key']替换为$\u POST[$key],其余代码应该可以工作though@jason我已经在你修改后的代码中添加了一些注释,并在我的回答中添加了一些注释。实际上,我曾经尝试过类似的方法,但失败得很惨。也许我可以根据自己的需要来调整。我试试看。这行有一个输入错误,对不起,echo“found$key=>”$\u POST[$key];将$\u POST['key']替换为$\u POST[$key],其余代码应该可以工作though@jason我已经在你修改后的代码中添加了一些注释,并在我的回答中添加了一些注释。这正是我最初试图做的。请参阅原始帖子中的编辑2。这正是我最初试图做的。请参阅原始帖子中的编辑2。不会$id=substr($field,8)代码>实际上是$id=substr($field,9)代码>因为它应该在第2个“e”之后开始,字符串中的第9个字符?嗯。。。这也不走运。看来我命中注定了(难道不是$id=substr($field,8);
实际上是$id=substr($field,9);
因为它应该在第二个“e”之后开始,字符串中的第九个字符?嗯……这也没什么好运气。看起来我命中注定了(