PHP通过特定的单词数组分隔字符串

时间:2017-06-19 12:40:50

标签: php

我在php中使用以下值进行数组

Array
(
    [0] => Clarithromycin 250mg/5ml oral susptake TWO 5ml spoonsful TWICE each day DISCARD REMAINING AFTER TEN DAYSSHAKE THE BOTTLE WELL BEFORE USING.SPACE THE DOSES EVENLY. KEEP TAKING UNTIL THE COURSE IS FINISHED, UNLESS YOU ARE TOLD TO STOP.
    [1] => Lactulose 3.1-3.7g/5ml oral solntake ONE to FOUR 5ml spoonsful TWICE each day when required (PRN : to be taken when necessary)
    [2] => Mirtazapine orodisp 30mg tabsOne To Be Taken At NightALLOW THE TABLETS TO DISSOLVE ON YOUR TONGUE, THEN SWALLOW WITH THE SALIVA. TAKE AFTER FOOD.WARNING: THIS MEDICINE MAY MAKE YOU FEEL SLEEPY. IF THIS HAPPENS, DO NOT DRIVE OR USE TOOLS OR MACHINES. DO NOT DRINK ALCOHOL.
    [3] => Senna 7.5mg/5ml oral soln SFTwo 5ml Spoonfuls To Be Taken At NightSHAKE THE BOTTLE WELL BEFORE USING.THIS MEDICINE MAY COLOUR YOUR URINE. THIS IS HARMLESS.
    [4] => SUDOCREM ANTISEPTIC HEALING CREAMas directedFOR EXTERNAL USE ONLY. (MD mean As directed)
    [5] => CIRCADIN MR 2MG TABSOne To Be Taken At NightSWALLOW WHOLE. DO NOT CHEW OR CRUSH.TAKE WITH OR JUST AFTER FOOD, OR A MEAL.WARNING: THIS MEDICINE MAY MAKE YOU FEEL SLEEPY. IF THIS HAPPENS, DO NOT DRIVE OR USE TOOLS OR MACHINES. DO NOT DRINK ALCOHOL.
    [6] => Memantine 10mg tabsOne To Be Taken Each Day
    [7] => Omeprazole gr 10mg capsOne To Be Taken Each DaySWALLOW WHOLE.DO NOT CHEW OR CRUSH.
    [8] => Senna 7.5mg tabsTwo To Be Taken At NightTHIS MEDICINE MAY COLOUR YOUR URINE. THIS IS HARMLESS.
)

我想用特定的词语分开医学和描述。例如(拿两个,一个拿,一个拿,等等......)

array
(
    [0] => Array
        (
            [medicine] => Clarithromycin 250mg/5ml oral susp
            [description] => take TWO 5ml spoonsful TWICE each day DISCARD REMAINING AFTER TEN DAYSSHAKE THE BOTTLE WELL BEFORE USING.SPACE THE DOSES EVENLY. KEEP TAKING UNTIL THE COURSE IS FINISHED, UNLESS YOU ARE TOLD TO STOP.
        )

    [1] => Array
        (
            [medicine] => Lactulose 3.1-3.7g/5ml oral soln
            [description] =>take ONE to FOUR 5ml spoonsful TWICE each day when required (PRN : to be taken when necessary)
        )

    [2] => Array
        (
            [medicine] => Mirtazapine orodisp 30mg tabs
            [description] => One To Be Taken At NightALLOW THE TABLETS TO DISSOLVE ON YOUR TONGUE, THEN SWALLOW WITH THE SALIVA. TAKE AFTER FOOD.WARNING: THIS MEDICINE MAY MAKE YOU FEEL SLEEPY. IF THIS HAPPENS, DO NOT DRIVE OR USE TOOLS OR MACHINES. DO NOT DRINK ALCOHOL.
        )
.
.
.
.
)

3 个答案:

答案 0 :(得分:0)

试试这个:

foreach ($array as $key => $value) {
    $explode = explode('separate', $value);
    $array[$key] = array(
        'medicine' => $explode[0],
        'description' => $explode[1]
    );
}

只需使用

分隔您想要分隔文字的单词即可“单独”

答案 1 :(得分:0)

//Main array
//Create array of words that you have to separate from
$arrOfsplittedData = [];
$intCount = 0;
foreach($arrOfMedicineAndDesc as $medicineAndDesc){
    $lowerCaseMedicineAndDesc = strtolower($medicineAndDesc);
    $splitMedicineAndDesc = multiexplode(array("oral", "tabsone", "cream", "capsone", "tabstwo"), $lowerCaseMedicineAndDesc);
    if($splitMedicineAndDesc){
        $arrOfsplittedData[$intCount]["medicine"] =          $splitMedicineAndDesc[0];
        $arrOfsplittedData[$intCount]["description"] = $splitMedicineAndDesc[1];
    }
    $intCount++;
}

function multiexplode ($delimiters,$string) {    
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}
echo "<pre>";
print_r($arrOfsplittedData);die;

答案 2 :(得分:0)

你可以用正则表达式来做这件事:

<?php
$array = array(
    "Clarithromycin 250mg/5ml oral susptake TWO 5ml spoonsful TWICE each day DISCARD REMAINING AFTER TEN DAYSSHAKE THE BOTTLE WELL BEFORE USING.SPACE THE DOSES EVENLY. KEEP TAKING UNTIL THE COURSE IS FINISHED, UNLESS YOU ARE TOLD TO STOP.",
    "Lactulose 3.1-3.7g/5ml oral solntake ONE to FOUR 5ml spoonsful TWICE each day when required (PRN : to be taken when necessary)",
    "Mirtazapine orodisp 30mg tabsOne To Be Taken At NightALLOW THE TABLETS TO DISSOLVE ON YOUR TONGUE, THEN SWALLOW WITH THE SALIVA. TAKE AFTER FOOD.WARNING: THIS MEDICINE MAY MAKE YOU FEEL SLEEPY. IF THIS HAPPENS, DO NOT DRIVE OR USE TOOLS OR MACHINES. DO NOT DRINK ALCOHOL.",
    "Senna 7.5mg/5ml oral soln SFTwo 5ml Spoonfuls To Be Taken At NightSHAKE THE BOTTLE WELL BEFORE USING.THIS MEDICINE MAY COLOUR YOUR URINE. THIS IS HARMLESS.",
    "SUDOCREM ANTISEPTIC HEALING CREAMas directedFOR EXTERNAL USE ONLY. (MD mean As directed)",
    "CIRCADIN MR 2MG TABSOne To Be Taken At NightSWALLOW WHOLE. DO NOT CHEW OR CRUSH.TAKE WITH OR JUST AFTER FOOD, OR A MEAL.WARNING: THIS MEDICINE MAY MAKE YOU FEEL SLEEPY. IF THIS HAPPENS, DO NOT DRIVE OR USE TOOLS OR MACHINES. DO NOT DRINK ALCOHOL.",
    "Memantine 10mg tabsOne To Be Taken Each Day",
    "Omeprazole gr 10mg capsOne To Be Taken Each DaySWALLOW WHOLE.DO NOT CHEW OR CRUSH.",
    "Senna 7.5mg tabsTwo To Be Taken At NightTHIS MEDICINE MAY COLOUR YOUR URINE. THIS IS HARMLESS."
);

$splitWords = [
    "take \w+?\b",
    "to be taken"
]; //Regex of what you want to split on

$regex = "/(".implode("|",$splitWords).")/";



$replaced = preg_replace($regex, "\n$1", $array); //Replace what you found with a newline + what you found

print_r(array_map(function ($v) { 
    $array = explode("\n",$v); 
    return [ 
        "medicine" => $array[0], 
        "description" => isset($array[1])?$array[1]:null
    ]; //If you're array is bigger than 2 elements you may need to loop here.
},$replaced)); //Split sentences on the newlines.

以下是一个例子:

http://sandbox.onlinephpfunctions.com/code/b2e2ceef17152e9a888a7b68fab3acb2e9f10fe3