1

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...

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the quick answer, but I only get the name and time with that pattern... Here is what I get from the first three meds: Array ( [0] => Array ( [name] => Versed [time] => 9:50 PM ) [1] => Array ( [name] => Lidocaine [time] => 9:50 PM ) [2] => Array ( [name] => Succinylcholine [time] => 9:50 PM ) )
@jreed121: That's odd because it works as expected for me (the output I posted is the actual output I get). There might be a twist that is not in your example, is the input data you tried the exact same thing as the one you posted?
yeah, it's the same. Here is an example that includes all of the potential combination I can think of:` 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: Nasal prongs Dosage: 200.00 MG Med Given: Vancomycin Med Admin Route: Oral Dosage: 20.00 MG Med Given: Fentanyl twowords Med Given: Dopamine Dosage: 200.00 MG Med Given: Oxygen - 7:23 PM Dosage: 2.00 L/MIN Med Given: Dextrose Med Admin Route: Intravenous Med Given: Gentamicin`
@jreed121: Can you update your question with this dataset? I don't get it, it works fine for me.
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.