Mega Code Archive

 
Categories / Php / Strings
 

Detecting ZIP Codes in Strings

<?php function detect_zipcode($string) {     return preg_match('/\b\d{5}(-\d{4})?\b/', $string); } echo '<pre>'; if (detect_zipcode('MD  11101-3883')) {     echo "Test 1: true\n"; } if (detect_zipcode('The zipcode 26623 is the area in which I grew up.')) {     echo "Test 2: true\n"; } if (detect_zipcode('The Phone Number is 301-555-1212')) {     echo "Test 3: true\n"; } if (detect_zipcode('426969-313109')) {     echo "Test 4: true\n"; } echo '</pre>'; ?>