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
is my syntax for using preg_match incorrect? does it not return a boolean?
Consider trying something like this instead of attempting to use preg_match. Preg_match is for matching against a regular expression, which you don't need here.
$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;
}
You can use an array for this, and PHP's in_array():
$outlimit_postal = array("V0R1A0","X0E0A0","J0M1V0");
and
if (in_array($order->delivery['postcode'], $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, i.e. 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.
If you cannot use the value, just add true
as a value.
Since it is already in a form of string, I would suggest just search the substring in your outlimit_postal string.
E.g.
if (strpos($order->delivery['postcode'], $outlimit_postal) === false) {
echo "no match in outlimit_postal";
}
else {
echo "match in outlimit_postal";
}