PHP 从数组中突出显示的关键字向后和向前将字符串剪切为 5 个单词

PHP Cut string to 5 words backwards and forward from highlighted keywords from array

I need to find in big strings each keyword, highlight them by and leave only five words before and after this keyword. You can see in screenshot i created php script which

Link 显示我拥有的和我需要的图像: http://dawid969.webd.pl/cut.jpg

我有代码 - PHP - 我创建了突出显示每个单词的功能,但我无法剪切字符串(围绕突出显示的单词向后 5 个单词和向前 5 个单词)当每个突出显示的单词是彼此相邻,那么我们不能切割字符串,只有当突出显示的单词之间的单词差异大于 10 个单词时,我们才切割字符串。

任何人都知道我如何才能提出最后一点? - 切割字符串?

<?php
$sentence = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again";

$arrayOfWords = array('explain', 'complete', 'pleasure');

echo "<b>Sentence:</b> ".$sentence;
echo '<br><br>';

echo "<b>Words to find (in array):</b> ";
print_r($arrayOfWords);

echo "<br><br>";

$look = explode(' ',$sentence);

foreach($look as $find){
    for($i=0;$i<count($arrayOfWords);$i++){
        $keyword = $arrayOfWords[$i];
        if(stripos($find, $keyword) !== false) {
            if(!isset($highlight)){ 
                $highlight[] = $find;
            } else { 
                if(!in_array($find,$highlight)){ 
                    $highlight[] = $find;
                } 
            }
        }   
    }
} 

if(isset($highlight)){ 
    foreach($highlight as $key => $replace){
        $sentence = str_replace($replace,'<b>'.$replace.'</b>',$sentence);
    } 
} 

echo "<b>Sentence formatted I have:</b> ".$sentence;

echo '<br><br>';

echo "<b>Sentence formated I need: </b> But I must <b>explain</b> to you how all this mistaken idea of denouncing <b>pleasure</b> and praising pain was born and I will give you a <b>complete</b> account of the system, and expound... ...one rejects, dislikes, or avoids <b>pleasure</b> itself, because it is pleasure,... ...not know how to pursue <b>pleasure</b> rationally encounter consequences that are...";
?>
<?php
$sentence = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again";

$arrayOfWords = array('explain', 'complete', 'pleasure');

echo "<b>Sentence:</b> ".$sentence;
echo '<br><br>';

echo "<b>Words to find (in array):</b> ";
print_r($arrayOfWords);

echo "<br><br>";

//replace this part

$look = explode(' ',$sentence);
$last_checked =0;
$i=0;
$highlight=false;
for(;$i<count($look);$i++){
    foreach ($arrayOfWords as $keyword){
        $find=$look[$i];
        if(stripos($find, $keyword) !== false) {
            $highlight =true;
            if($i-$last_checked>10){
                $j = ($last_checked ==0)?0:  $last_checked+ 5;
                $dots=true;

                for(;$j<$i -5;$j++) {
                    if($dots){
                        $look[$j]= "...";
                        $dots=false;
                    }else
                        $look[$j]= "";
                }
            }
            $last_checked =$i;
        }
    }
}


$sentence=implode(" ",$look);


if(isset($highlight)){
    foreach($arrayOfWords as $key => $replace){
        $sentence = str_replace($replace,'<b>'.$replace.'</b>',$sentence);
    }
}

echo "<b>Sentence formatted I have:</b> ".$sentence;

echo '<br><br>';

echo "<b>Sentence formated I need: </b> But I must <b>explain</b> to you how all this mistaken idea of denouncing <b>pleasure</b> and praising pain was born and I will give you a <b>complete</b> account of the system, and expound... ...one rejects, dislikes, or avoids <b>pleasure</b> itself, because it is pleasure,... ...not know how to pursue <b>pleasure</b> rationally encounter consequences that are...";
?>

我希望这能给你一个观点,我没有关注确切的数字。

我的正则表达式模式可能需要一点 "staring at" 但基本上它们在每个找到的关键字的两边最多匹配 5 "words"(松散地使用术语)。

句子首先被分成一个子串数组,子串要么包含关键字,要么不包含关键字。叫var_export($chunks);看看我的意思

然后对每个元素进行条件处理。如果元素:

  • 包含关键词,底气动作已出
  • 恰好是一个space,该元素保持不变。
  • 不包含关键字且是第一个或最后一个元素,则变为...
  • 不包含关键字且出现在句子中间,变为... ...

代码:(Demo)

$sentence = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again";
$arrayOfWords=['explain','complete','pleasure'];
$pattern_core=implode('|',array_map(function($v){return preg_quote($v,'/');},$arrayOfWords));  // escape regex-impacting characters and pipe together

// split the sentence on keywords with upto 5 "words" padding, retain the delimiters
$chunks=preg_split("/((?:\S+\s+){0,5}\S*(?:".$pattern_core.")\S*(?:\s+\S+){0,5})/iu",$sentence,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

$last_chunk=sizeof($chunks)-1;
foreach($chunks as $i=>&$chunk){  // make $chunk modifiable with & symbol
    $chunk=preg_replace("/{$pattern_core}/iu",'<b>[=10=]</b>',$chunk,-1,$count);
    if(!$count && $chunk!=' '){  // if contains no keyword and not a single space...
        if($i==0 || $i==$last_chunk){  // single set of dots at beginning and end of sentence
            $chunk='...';
        }else{
            $chunk='... ...';  // double set of dots in the middle of sentence
        }
    }
}
echo implode($chunks);

输出:

But I must <b>explain</b> to you how all this mistaken idea of denouncing <b>pleasure</b> and praising pain was born... ...I will give you a <b>complete</b> account of the system, and... ...one rejects, dislikes, or avoids <b>pleasure</b> itself, because it is <b>pleasure</b>,... ...not know how to pursue <b>pleasure</b> rationally encounter consequences that are...