Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created April 26, 2013 07:37
Show Gist options
  • Select an option

  • Save joshuaadickerson/5465556 to your computer and use it in GitHub Desktop.

Select an option

Save joshuaadickerson/5465556 to your computer and use it in GitHub Desktop.
Match the state code with the string. Should match somerset but not "me" in there.
<?php
// Should match:
$string = 'somerset nj';
$string = 'nj somerset';
$string = 'nj';
$string = 'NJ ';
// Should NOT match
$string = 'somersetnj';
$string = 'somerset';
preg_match('/\s?(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])\s?/i', $string, $matches);
@joshuaadickerson

Copy link
Copy Markdown
Author

Just in case this turns up in searches
http://codepad.viper-7.com/crBlzC

preg_match('/(?<!\S)(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])(?!\S)/i', $string, $matches);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment