php 从字符串中去除特定单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9342149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strip specific words from string
提问by Y Kal
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”之类的禁止词的词。
回答by cmbuckley
For this you can use preg_replaceand 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.
编辑:示例。
回答by Stefan Leever
The str_replace also supports the input of an array. This means that you could use it in the following way:
str_replace 还支持数组的输入。这意味着您可以通过以下方式使用它:
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 ". As you would only remove the real words and not parts of words.
要删除设置中的正确单词,我建议在“或”和“where”周围添加空格。因为你只会删除真实的单词而不是单词的一部分。
I hope this helps.
我希望这有帮助。
回答by elrado
TRY:
尝试:
$string = preg_replace("\b$word\b", "", $string);
回答by Ankur Goel
$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
但它会添加空格来代替这些单词,因此您需要再次使用替换来删除双空格并删除它们
回答by Kenny Ming
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”