简体   繁体   English

从字符串中删除特定的单词/短语

[英]Strip specific words/phrases from string

I want to remove some specific words and phrases from a string and I followed the discussion at Strip specific words from string . 我想从一个字符串中删除一些特定的单词和短语,我跟在讨论从字符串中去除特定的词

I have created an array of forbidden words: $wordlist = array("bayes", "full", "greater than"); 我创建了一个禁止单词数组:$ wordlist = array(“ bayes”,“ full”,“ greater than”);

My example string is: 我的示例字符串是:

$string= "my bayesian results are full of bayes and greater than anything else";

If I apply the following code: 如果我应用以下代码:

foreach ($wordlist as &$word) {
    $word = '/\b' . preg_quote($word, '/') . '\b/';
}
$string = preg_replace($wordlist, '', $string);

the result is: my bayesian results are of and greater than anything else [removed word: bayes, full] 结果是:我的贝叶斯结果是,并且比其他任何结果都要大[移除词:bayes,full]

whereas I want the result to be: my results are of and anything else [removed word: bayes, bayesian, full, greater than] 而我希望结果是:我的结果是以及其他内容[删除的词:贝叶斯,贝叶斯,完整,大于]

Note that I want that the forbidden word 'bayes' should remove the word 'bayesian' as well. 请注意,我希望禁止的单词“ bayes”也应删除单词“ bayesian”。

Try adding .*? 尝试添加.*? before the ending boundary. 在结束边界之前。 It would match non-greedily till the boundary is reached. 它会非贪婪地匹配,直到达到边界为止。
Like this - 像这样 -

$word = '/\b' . preg_quote($word, '/') . '.*?\b/';
                                          ^^^

It outputs - 它输出-

my  results are  of  and  anything else

Maybe this: 也许这样:

$string = "my bayesian results are full of bayes and greater than anything else";
$forbidden = array('/bayes(\w+)/','/full(\w+)/');
$result = preg_replace($forbidden , array(), $string);
print_r($result);

This code takes the string, tries to match an array of regex patterns ($forbidden) to the string and replaces them with an empty value. 此代码获取字符串,尝试将正则表达式模式数组($ forbidden)与该字符串匹配,并将其替换为空值。

It looks for words starting with bayes or full and stops looking when it finds whitespace. 它查找以贝叶斯开头或完整开头的单词,并在找到空白时停止查找。

First split the string into words then check each word against each banned word and remove it if necessary. 首先将字符串分成多个单词,然后将每个单词与每个禁止的单词进行比较,并在必要时将其删除。

$string = "my bayesian results are full of bayes and greater than anything else";
$banned_words = array("bayes", "full", "greater than");
$words = explode(' ',$string);

foreach($words as $key => $word) {
    foreach($banned_words as $banned_word) {
        if(false !== stripos($word,$banned_word)) {
            unset($words[$key]);
            break;
        }
    }
}

$string = implode(' ',$words);

Finally remove any 1+ word full matches 最后删除所有超过1个单词的完整匹配项

print str_ireplace($banned_words,'',$string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM