简体   繁体   English

检查邮政编码是否与预定义列表匹配

[英]checking if a postal code matches from a predefined list

I want to create a list of postal codes that are out of range for free shipping. 我想创建一个邮政编码列表,这些邮政编码超出了免费送货范围。 I am not sure if I need an array to do this so I first created a variable: 我不确定是否需要数组来执行此操作,因此我首先创建了一个变量:

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";

My first issue is I am not sure if I need to create an array or store these in my database as separate rows. 我的第一个问题是我不确定是否需要创建一个数组或将它们作为单独的行存储在数据库中。 Or if I can just keep them in a single variable list to check against like I have. 或者,如果我可以将它们保留在单个变量列表中,以像我一样进行检查。

Anyhow next I want to match the persons postal code to check if they are in the list: 无论如何,接下来我要匹配人员的邮政编码,以检查他们是否在列表中:

  if ($this->enabled == true)   {

            if (preg_match($order->delivery['postcode'], $outlimit_postal )) {
                $this->enabled = false;

            } 

However I got an error: 但是我得到一个错误:

Warning: preg_match(): Empty regular expression 警告:preg_match():空正则表达式

is my syntax for using preg_match incorrect? 我使用preg_match的语法不正确吗? does it not return a boolean? 它不返回布尔值吗?

You can use an array for this, and PHP's in_array() : 您可以为此使用数组,以及PHP的in_array()

$outlimit_postal = array("V0R1A0","X0E0A0","J0M1V0");

and

if (in_array($order->delivery['postcode'], $outlimit_postal) )

Consider trying something like this instead of attempting to use preg_match. 考虑尝试这样的事情,而不是尝试使用preg_match。 Preg_match is for matching against a regular expression, which you don't need here. 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;

            } 

Since it is already in a form of string, I would suggest just search the substring in your outlimit_postal string. 由于它已经是字符串形式,因此建议您只搜索outlimit_postal字符串中的子字符串。

Eg 例如

if (strpos($order->delivery['postcode'], $outlimit_postal) === false) {
    echo "no match in outlimit_postal";
}
else {
    echo "match in outlimit_postal";
}

Store it as a string but check it as an array.. 将其存储为字符串,但将其检查为数组。

$outlimit_postal = explode(",", $outlimit_postal);
if(in_array($post_code, $outlimit_postal)) echo "Your post code is in the list";

Use an array. 使用数组。 Make the postal code the array key. 使邮政编码成为阵列键。 Put anything that defines the non-free shipping as the value, ie the amount of shipping in absolute or relative terms, or some entire business object being configured to calculate the correct shipping. 将定义非免费送货的所有内容(例如,以绝对或相对术语表示的送货量)或配置为计算正确送货的整个业务对象放入值中。

$shipping['V0R1A0'] = 1.5; // e.g. as absolute amount

Checking if non-free shipping has to be made: 检查是否必须进行非免费送货:

if (isset($shipping[$postalcode])) {...

Those suggesting solutions with in_array() should be reminded that searching inside an array is slow - that's what array keys are made for. 应该提醒那些使用in_array()提出解决方案的人,即在数组内部进行搜索的速度很慢-这就是数组键的作用。

If you cannot use the value, just add true as a value. 如果您不能使用该值,只需将true添加为值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM