我想创建一个超出免费送货范围的邮政编码列表。我不确定我是否需要一个数组才能这样做,所以我首先创建了一个变量:
outlimit_postal = "V0R1A0,X0E0A0,J0M1V0,P0V1B0,X0A0A0,X0C0E0,P0L1A0,J0M1X0,X0C0A0,X0B2A0,P0V1E0,R0B0A0,P0V1G0,S0J0H0,A0K1N0,V0P1B0,R0C0J0,R0B0B0,X0B0C0,X0A0C0,P0V1J0,X0C0B0,G0G1G0,X0A0E0,X0E1L0,X0C0C0,V0N1M0,P0V1N0,X0E0G0,P0T1L0,X0A0G0,S0J0W0,P0L1H0,T0P1B0,X0E0H0,P0V1W0,T0H1R0,G0G1M0,X0B1J0,R0B0M0,R0B0N0,R0B0P0,X0A0J0,X0A0K0,G0G1N0,V0V1A0,X0E0S0,A0P1G0,X0A0L0,J0M1M0,X0A0H0,X0A1H0,R0B0T0,J0M1H0,J0M1N0,J0M1K0,J0M1A0,P0V1Y0,P0L1S0,G0G1S0,P0V3G0,X0A0N0,V0N2B0,P0V1Z0,V0V1C0,V0T1L0,X0B1K0,X0B0E0,J0M1C0,J0M1G0,V0P1J0,G0G1T0,R0B2E0,P0T1Z0,V0V1H0,R0B0V0,X0E1A0,A0P1J0,V0P1L0,P0V3B0,G0G2C0,A0P1L0,A0P1A0,R0B0Z0,X0E0V0,P0V2G0,V0T1P0,P0T2L0,Y0B1N0,V0V1E0,V0N3S0,R0B1C0,X0A0R0,R0B2G0,X0E1N0,P0L2H0,P0V2L0,X0A0S0,P0V3E0,R0B0Z0,G0G2Y0,A0P1N0,R0B1G0,J0M1P0,X0A0B0,J0M1J0,X0E1R0,X0C0G0,R0B1H0,V0P1P0,X0C0H0,X0A0V0,A0P1P0,P0V2P0,X0E0Z0,J0M1S0,P0V1V0,X0A0W0,R0B1K0,V0P1S0,R0B1N0,G0G2R0,R0B1J0,R0B2H0,S0J2R0,V0P1V0,V0N3H0,P0T3B0,V0P1W0,R0B2C0,X0B1B0,J0M1T0,G0G2W0,X0E1Z0,V0J3N0,X0E1C0,X0E0K0,J0M1Y0,S0J2W0,R0B1Z0,V0J3B0,P0V2Y0,P0T3A0,X0E1W0,X0E1P0,X0C0J0,A0K5V0,S0J3C0,P0V2Z0,R0B2B0";
我的第一个问题是我不确定是否需要创建一个数组或将它们作为单独的行存储在我的数据库中。或者,如果我可以将它们保存在单个变量列表中,以便像我一样进行检查。
无论如何,接下来我想匹配人员邮政编码以检查他们是否在列表中:
if ($this->enabled == true) {
if (preg_match($order->delivery['postcode'], $outlimit_postal )) {
$this->enabled = false;
}
但是我收到了一个错误:
警告:preg_match():清空正则表达式
是否使用preg_match的语法不正确?它不返回布尔值吗?
答案 0 :(得分:2)
您可以使用数组,以及PHP的in_array():
$outlimit_postal = array("V0R1A0","X0E0A0","J0M1V0");
和
if (in_array($order->delivery['postcode'], $outlimit_postal) )
答案 1 :(得分:2)
考虑尝试这样的事情,而不是尝试使用preg_match。 Preg_match用于匹配正则表达式,这里不需要。
$outlimit_postal = "V0R1A0,X0E0A0,J0M1V0,P0V1B0,X0A0A0,X0C0E0,P0L1A0,J0M1X0,X0C0A0,X0B2A0,P0V1E0,R0B0A0,P0V1G0,S0J0H0,A0K1N0,V0P1B0,R0C0J0,R0B0B0,X0B0C0,X0A0C0,P0V1J0,X0C0B0,G0G1G0,X0A0E0,X0E1L0,X0C0C0,V0N1M0,P0V1N0,X0E0G0,P0T1L0,X0A0G0,S0J0W0,P0L1H0,T0P1B0,X0E0H0,P0V1W0,T0H1R0,G0G1M0,X0B1J0,R0B0M0,R0B0N0,R0B0P0,X0A0J0,X0A0K0,G0G1N0,V0V1A0,X0E0S0,A0P1G0,X0A0L0,J0M1M0,X0A0H0,X0A1H0,R0B0T0,J0M1H0,J0M1N0,J0M1K0,J0M1A0,P0V1Y0,P0L1S0,G0G1S0,P0V3G0,X0A0N0,V0N2B0,P0V1Z0,V0V1C0,V0T1L0,X0B1K0,X0B0E0,J0M1C0,J0M1G0,V0P1J0,G0G1T0,R0B2E0,P0T1Z0,V0V1H0,R0B0V0,X0E1A0,A0P1J0,V0P1L0,P0V3B0,G0G2C0,A0P1L0,A0P1A0,R0B0Z0,X0E0V0,P0V2G0,V0T1P0,P0T2L0,Y0B1N0,V0V1E0,V0N3S0,R0B1C0,X0A0R0,R0B2G0,X0E1N0,P0L2H0,P0V2L0,X0A0S0,P0V3E0,R0B0Z0,G0G2Y0,A0P1N0,R0B1G0,J0M1P0,X0A0B0,J0M1J0,X0E1R0,X0C0G0,R0B1H0,V0P1P0,X0C0H0,X0A0V0,A0P1P0,P0V2P0,X0E0Z0,J0M1S0,P0V1V0,X0A0W0,R0B1K0,V0P1S0,R0B1N0,G0G2R0,R0B1J0,R0B2H0,S0J2R0,V0P1V0,V0N3H0,P0T3B0,V0P1W0,R0B2C0,X0B1B0,J0M1T0,G0G2W0,X0E1Z0,V0J3N0,X0E1C0,X0E0K0,J0M1Y0,S0J2W0,R0B1Z0,V0J3B0,P0V2Y0,P0T3A0,X0E1W0,X0E1P0,X0C0J0,A0K5V0,S0J3C0,P0V2Z0,R0B2B0";
$allowed_postal_codes = explode(',', $outlimit_postal);
if ($this->enabled == true) {
if (in_array($order->delivery['postcode'], $allowed_postal_codes)) {
$this->enabled = false;
}
答案 2 :(得分:1)
由于它已经是字符串形式,我建议只搜索outlimit_postal字符串中的子字符串。
E.g。
"HandleBars": {
"something": "value"
}
答案 3 :(得分:0)
将其存储为字符串,但将其检查为数组..
$outlimit_postal = explode(",", $outlimit_postal);
if(in_array($post_code, $outlimit_postal)) echo "Your post code is in the list";
答案 4 :(得分:0)
使用数组。使邮政编码成为数组键。将定义非免费送货的任何内容作为价值,即以绝对或相对条款的运输金额,或者配置一些整个业务对象来计算正确的运输。
$shipping['V0R1A0'] = 1.5; // e.g. as absolute amount
检查是否必须免运费:
if (isset($shipping[$postalcode])) {...
那些建议使用in_array()
的解决方案的人应该提醒一下,在数组内部搜索速度很慢 - 这就是为数组创建的数组键。
如果您无法使用该值,只需将true
添加为值。