Sarah Greenwood
Sarah Greenwood

Reputation: 1

Case insensitive in array

We have the following code in opencart that specifies postcodes that do not receive free postage:

foreach ($results as $result) {
  if ($this->config->get($result['code'] . '_status')) {
    $noFreeDelivery = false;

    if ($result['code'] == 'free') {
      $disallowedFreePostcodes = array(
        'IV', 'BT', 'HS', 'KA27', 'KA28', 'KW', 'PA20', 'PA21', 'PA22', 'PA23', 'PA24', 'PA25', 'PA26', 'PA27', 'PA28', 'PA29', 'PA30', 'PA31', 'PA32', 'PA33', 'PA34', 'PA35', 'PA36', 'PA37', 'PA38', 'PA39', 'PA40', 'PA41', 'PA42', 'PA43', 'PA44', 'PA45', 'PA46', 'PA47', 'PA48', 'PA49', 'PA60', 'PA61', 'PA62', 'PA63', 'PA64', 'PA65', 'PA66', 'PA67', 'PA68', 'PA69', 'PA70', 'PA71', 'PA72', 'PA73', 'PA74', 'PA75', 'PA76', 'PA77', 'PA78', 'PH17', 'PH18', 'PH19', 'PH20', 'PH21', 'PH22', 'PH23', 'PH24', 'PH25', 'PH26', 'PH30', 'PH31', 'PH32', 'PH33', 'PH34', 'PH35', 'PH36', 'PH37', 'PH38', 'PH39', 'PH40', 'PH41', 'PH42', 'PH43', 'PH44', 'PH49', 'PH50', 'ZE', 'BT', 'IM', 'TR21', 'TR22', 'TR23', 'TR24', 'TR25', 'AB31', 'AB33', 'AB34', 'AB35', 'AB36', 'AB37', 'AB38', 'AB45', 'AB52', 'AB53', 'AB54', 'AB55', 'AB56', 'FK', 'Ph33'
      );

      foreach ($disallowedFreePostcodes as $postcode) {
        $range = explode('-', $postcode);

        if (array_key_exists(1, $range)) { 
          $start = str_split($range[0], 2);

          foreach (range($start[1], $range[1]) as $postcodeSuffix) {
            $code = $start[0].$postcodeSuffix;

            if (strpos($this->session->data['shipping_address']['postcode'], $code) === 0) {
              $noFreeDelivery = true;
              break;
            }
          }

          continue;
        }

        if (strpos($this->session->data['shipping_address']['postcode'], $range[0]) === 0) {
          $noFreeDelivery = true;
          break;
        }
      }
    }
  }

The problem I'm facing is that if someone, for example, entered pa22 (lowercase) or Pa22 (mixed cases) they would still be offered the free shipping. Can someone suggest a way in which I can get the code to discount case without having to add every possible input to the array?

TIA

Upvotes: 0

Views: 66

Answers (1)

Ramin Rezazadeh
Ramin Rezazadeh

Reputation: 336

You must convert array values to lower and convert entered value to lower too, after that you can check easily.

For example,in this code snippet:

strpos($this->session->data['shipping_address']['postcode'], $code)

you must do like this:

strpos(strtolower($this->session->data['shipping_address']['postcode']), strtolower($code))

Upvotes: 1

Related Questions