CodeIgniter Forums

Full Version: regex pattern needs improvement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to use one pattern that handles having a date in the subject and not having a date in the subject. Can't get it to work both ways. Also the 9 digit number appears twice, which is quite a mystery to me.

Code:
$subject = " DRUG COURT REVIEW S18 151905861 State Felony";
$subject2 = "    10:00 AM      DRUG COURT REVIEW                 SSL 151907220 State Felony";
echo "<br /><br />SUBJECT:".$subject."<br />";
$pattern = "/(\d{2}:\d{2}\s(AM|PM)\s)(.*)(\w{3,4}\s)(([0-9]{9})\s)(.*)/";  // works with a date
$pattern2 = "/(.*)(\w{3,4}\s)(([0-9]{9})\s)(.*)/";                         // works without date
$retval = preg_match($pattern2,$subject,$matches, PREG_OFFSET_CAPTURE);
echo "<br />RESULT COUNT>>> ".count($matches)."<<<<br />";  
if ($retval==1) {
 print_r($matches);
 
 if (isset($matches[1][0]))
   echo "<br />RESULT>>> ".$matches[1][0]."<<<";  
 if (isset($matches[2][0]))
   echo "<br />RESULT>>> ".$matches[2][0]."<<<";      
 if (isset($matches[3][0]))
   echo "<br />RESULT>>> ".$matches[3][0]."<<<";    
 if (isset($matches[4][0]))
   echo "<br />RESULT>>> ".$matches[4][0]."<<<";  
 if (isset($matches[5][0]))
   echo "<br />RESULT>>> ".$matches[5][0]."<<<";
 if (isset($matches[6][0]))
   echo "<br />RESULT>>> ".$matches[6][0]."<<<";
 if (isset($matches[7][0]))
   echo "<br />RESULT>>> ".$matches[7][0]."<<<";    
}
else
 echo "No match! <br />";
RESULT>>> 10:00 AM <<<
RESULT>>> AM<<<
RESULT>>> DRUG COURT REVIEW <<<
RESULT>>> SSL <<<
RESULT>>> 151907220 <<<
RESULT>>> 151907220<<<
RESULT>>> State Felony<<<