Example of the haystack:
INTERVENTIONS:
---------------------
Med Given: Versed - 9:50 PM Med Admin Route: Intravenous Dosage: 20.00 MG
Med Given: Lidocaine - 9:50 PM Med Admin Route: Intravenous Dosage: 150.00 MG
Med Given: Succinylcholine - 9:50 PM Med Admin Route: Intravenous Dosage: 200.00 MG
Med Given: Oxygen - 7:23 PM Dosage: 2.00 L/MIN
Med Given: Vancomycin
Med Given: Fentanyl
Med Given: Dopamine
Med Given: Dextrose
Med Given: Gentamicin
As you cans see, sometimes there are times ( - H:MM AM/PM), sometimes "Med Admin Route: ..." and "Dosage: ...", I always want the name (Versed, Oxygen, etc) and if available - the time (H:MM AM/PM), route (Intravenous, Oral, etc) and dosage (20.00 MG, 2.00 L/MIN, etc) all stored in an array. I've thought that I've had it in the past but when I throw a different haystack at it it fails... Also note that it appears that sometimes there is a tab instead of a space between the variables like time-Admin or Admin-Dosage...
preg_match_all('~Med Given: ((?:(?!-\s*\d{1,2}:\d{1,2} (?:A|P)M|Med Admin Route:|Dosage:|$).)+)(?:\s*-\s*(.*?(?:A|P)M))?(?:\s*Med Admin Route:((?:(?!Dosage:|$).)+))?(?:\s*Dosage:\s*(.*))?~',$content,$matches);
Got the job done thanks to the guys at phpfreaks.com
Luckily for you, I have some time on my hands during my lunch break :)
In regex, ?
after an expression means that it will accept one or zero occurences. Per example:
preg_match('/^(foo)?bar/', 'foobar'); // 1
preg_match('/^(foo)?bar/', 'bar'); // also 1
In your case, it is a little hard to do by regex, but feasible anyway:
preg_match_all('/Med Given: (?<name>[A-Za-z ]+)(- (?<time>[0-9:]+ (AM|PM)))?( +Med Admin Route: (?<route>\w+))?( +Dosage: (?<dosage>.*))?/', $data, $matches);
Then post-process the array:
$result = array();
foreach ($matches['name'] as $key => $name) {
$result = array('name'=>$name);
if (!empty($matches['time'][$key])) $result['time'] = $matches['time'][$key];
if (!empty($matches['route'][$key])) $result['route'] = $matches['route'][$key];
if (!empty($matches['dosage'][$key])) $result['dosage'] = $matches['dosage'][$key];
$results[] = $result;
}
print_r($results);
This should give you:
Array
(
[0] => Array
(
[name] => Versed
[time] => 9:50 PM
[route] => Intravenous
[dosage] => 20.00 MG
)
[1] => Array
(
[name] => Lidocaine
[time] => 9:50 PM
[route] => Intravenous
[dosage] => 150.00 MG
)
[2] => Array
(
[name] => Succinylcholine
[time] => 9:50 PM
[route] => Intravenous
[dosage] => 200.00 MG
)
[3] => Array
(
[name] => Oxygen
[time] => 7:23 PM
[dosage] => 2.00 L/MIN
)
[4] => Array
(
[name] => Vancomycin
)
[5] => Array
(
[name] => Fentanyl
)
[6] => Array
(
[name] => Dopamine
)
[7] => Array
(
[name] => Dextrose
)
[8] => Array
(
[name] => Gentamicin
)
)
The only issue here is the "Med Admin Route" bit. It must be a single word (i.e.: no spaces).