0

I have the following code which works to a point, it attempts to exclude UK highlands and Islands from UK postcodes however 2 lines throw the error commented bellow why is this?

$strPostCode = 'AB393HX';

if(!preg_match('/^(ZE|IV|TR|GY|JE)[0-9]/',$strPostCode)

   && !preg_match('/^(AB)[31-38]{2}/',$strPostCode)

   && !preg_match('/^(AB)[41-45]{2}/',$strPostCode)
   && !preg_match('/^(AB)[55-56]{2}/',$strPostCode)

   && !preg_match('/^(PA)[20-78]{2}/',$strPostCode)

   && !preg_match('/^(PH)[15-99]{2}/',$strPostCode)

   && !preg_match('/^(FK)[17-21]{2}/',$strPostCode) // Compilation failed: range out of order in character class at offset 9 

   && !preg_match('/^(KA)[27-28]{2}/',$strPostCode) // Compilation failed: range out of order in character class at offset 9 

   && !preg_match('/^(HS)[1-9]{1}/',$strPostCode)

   && !preg_match('/^(KW)[0-9]{1}/',$strPostCode)

   && !preg_match('/^(KW)[10-17]{2}/',$strPostCode)
   ){
    echo $strPostCode . ' is good';
   }
    else{
      echo $strPostCode . ' is bad';
   }
futureweb
  • 442
  • 4
  • 12
  • 1
    `[17-21]` is invalid because of range `[7-2]`. RHS should be higher in ASCII table – anubhava Nov 20 '18 at 13:57
  • 1
    I think that's only working by luck for the others, regular expressions match characters not numbers so `[17-21]` doesn't match what you think it'll match: https://stackoverflow.com/questions/3148240/why-doesnt-01-12-range-work-as-expected – CD001 Nov 20 '18 at 13:57
  • 2
    Correct expression is: `1[789]|2[01]` – anubhava Nov 20 '18 at 13:58
  • 2
    `[41-45]` does not match numbers from `41` to `45`. It matches a single digit, `4`, `1`, `2`, `3` and `5`. The `[7-2]` range is invalid. – Wiktor Stribiżew Nov 20 '18 at 13:58

0 Answers0