简体   繁体   English

从字符串中去除特定单词

[英]Strip specific words from string

I want to strip whole words from a string wherever they are inside the string.我想从字符串中的任何位置删除整个单词。

I have created an array of forbidden words:我创建了一系列禁用词:

$wordlist = array("or", "and", "where", ...)

Now I strip the words:现在我去掉这些词:

foreach($wordlist as $word)
$string = str_replace($word, "", $string);

The problem is that the above code also strips words that contain the forbidden words like "sand" or "more".问题是上面的代码还删除了包含诸如“sand”或“more”之类的禁止词的词。

For this you can use preg_replace and word boundaries:为此,您可以使用preg_replace和单词边界:

$wordlist = array("or", "and", "where");

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

$string = preg_replace($wordlist, '', $string);

EDIT : Example .编辑示例

The str_replace also supports the input of an array. str_replace 还支持数组的输入。 This means that you could use it in the following way:这意味着您可以通过以下方式使用它:

str_replace(array("word1", "word2"), "", $string);

This means that all the words existing in the array will be replaced by an empty string.这意味着数组中存在的所有单词都将替换为空字符串。 If you want specific replacements you can create an array of replacements as well.如果你想要特定的替换,你也可以创建一个替换数组。

To remove the correct words in your setup I would advise to add spaces around " or " and " where ".要删除设置中的正确单词,我建议在“或”和“where”周围添加空格。 As you would only remove the real words and not parts of words.因为你只会删除真实的单词而不是单词的一部分。

I hope this helps.我希望这有帮助。

尝试:

 $string = preg_replace("\b$word\b", "", $string);
$areaname = str_replace(array("to", "the","a","an","in","by","but","are","is","had","have","has"),'',$areaname);

i used it for this and works fine我用它来做这个并且工作正常

but it will add spaces in place of these words,so you need to use replace again to chk double spaces and remove them但它会添加空格来代替这些单词,因此您需要再次使用替换来删除双空格并删除它们

If you are using utf-8 characters you will need to add /u to the regex expression如果您使用的是 utf-8 字符,则需要将 /u 添加到正则表达式

$string = "regadío";
$omit = ["a", "y", "o"];
$string = preg_replace('/\b(' . implode('|', $omit) . ')\b/u', '', $string);

Without the /u it will remove the "o" from "regadío" -> "regadí"如果没有 /u,它将从“regadío”->“regadí”中删除“o”

I had the same problem and fixed it like this:我遇到了同样的问题并像这样修复它:

$filterWords = ['a','the','for'];
$searchQuery = 'a lovely day';
$queryArray = explode(' ', $searchQuery);
$filteredQuery = array_diff($queryArray, $filterWords);

$filteredQuery will contain ['lovely','day'] $filteredQuery 将包含 ['lovely','day']

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

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