Mega Code Archive

 
Categories / Php / Strings
 

Capturing with preg_match()

<? if (preg_match('/^(\d{5})(-\d{4})?$/',$_POST['zip'],$matches)) {     print "$matches[0] is a valid US ZIP Code\n";     print "$matches[1] is the five-digit part of the ZIP Code\n";     if (isset($matches[2])) {         print "The ZIP+4 is $matches[2];";     } else {         print "There is no ZIP+4";     } } $is_bold = preg_match('@<b>([^<]+)</b>@',$html,$matches); if ($is_bold) {     print "The bold text is: $matches[1]"; } ?>