×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Valentin Tkachev
Added: Sep 16, 2017 6:16 PM
Modified: Sep 16, 2017 6:17 PM
Views: 4
Good practice of writing readable regular expressions
  1. $address = 'One Infinite Loop, Cupertino 95014';
  2. $cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(?<city>.+?)\s*(?<zipCode>\d{5})?$/';
  3. preg_match($cityZipCodeRegex, $address, $matches);
  4.  
  5. saveCityZipCode($matches['city'], $matches['zipCode']);