\n"; else echo "This line does NOT contain a course number!
\n"; // Sometimes we are more interested in knowing WHAT matched, not just // that it did or didn't match: if (preg_match("/([A-Z]{4})-(\d{4})-([A-Z])/",$line,$result)) { echo "The course number is: ".$result[0] . "
\n"; echo "The course prefix is: ".$result[1] . "
\n"; echo "The course digits are: ".$result[2] . "
\n"; echo "The course section is: ".$result[3] . "
\n"; } if (preg_match("/([A-Z]{4})-(\d{4})-[A-Z]/",$line,$result)) { // I took one () away echo "The course number is: ".$result[0] . "
\n"; echo "The course prefix is: ".$result[1] . "
\n"; echo "The course digits are: ".$result[2] . "
\n"; echo "The course section is: ".$result[3] . "
\n"; } // Sometimes we want to split a string on a regular expression ... for // example, we may want to split the above string on spaces: $result= preg_split("/\s\s+/",$line); foreach ($result as $value) { echo "split: ".$value."
\n"; } // In this particular application a blind split on spaces probably isn't // what we want, so we'd need to apply a more careful split using // preg_match ... $matched= preg_match("/^([A-Z]{4})-(\d{4})-([A-Z])\s+(.+)\s+([A-Z]{3})\s+(\d\d:\d\d[AP])\s+(\d\d:\d\d[AP])\s+([A-Z]+)\s+(\d+)\s+(.+)$/",$line,$result); if ($matched) { foreach ($result as $value) { echo "info: ".$value."
\n"; } } ?>