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

试试这个:

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

只需通过您要分隔文本的单词编辑"分隔">

//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;

你可以狡猾地使用正则表达式来做到这一点:

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

相关内容

  • 没有找到相关文章

最新更新