将停用词放入字符串中

发布于 2025-01-02 12:05:02 字数 376 浏览 1 评论 0原文

我想在 PHP 中创建一个函数,当它发现字符串中有一些坏词时,它会返回 true。

这是一个示例:

function stopWords($string, $stopwords) {
if(the words in the stopwords variable are found in the string) {
return true;
}else{
return false;
}

请假设 $stopwords 变量是一个值数组,例如:

$stopwords = array('fuc', 'dic', 'pus');

我该怎么做?

谢谢

I want to create a function in PHP that will return true when it finds that in the string there are some bad words.

Here is an example:

function stopWords($string, $stopwords) {
if(the words in the stopwords variable are found in the string) {
return true;
}else{
return false;
}

Please assume that $stopwords variable is an array of values, like:

$stopwords = array('fuc', 'dic', 'pus');

How can I do that?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2025-01-09 12:05:02

使用 strpos 函数。

// the function assumes the $stopwords to be an array of strings that each represent a
//  word that should not be in $string
function stopWords($string, $stopwords) 
{  
     // input parameters validation excluded for brevity..

     // take each of the words in the $stopwords array
     foreach($stopwords as $badWord)
     {
         // if the $badWord is found in the $string the strpos will return non-FALSE        
         if(strpos($string, $badWord) !== FALSE))
           return TRUE;
     }
     // if the function hasn't returned TRUE yet it must be that no bad words were found
     return FALSE;
 }

Use the strpos function.

// the function assumes the $stopwords to be an array of strings that each represent a
//  word that should not be in $string
function stopWords($string, $stopwords) 
{  
     // input parameters validation excluded for brevity..

     // take each of the words in the $stopwords array
     foreach($stopwords as $badWord)
     {
         // if the $badWord is found in the $string the strpos will return non-FALSE        
         if(strpos($string, $badWord) !== FALSE))
           return TRUE;
     }
     // if the function hasn't returned TRUE yet it must be that no bad words were found
     return FALSE;
 }
流殇 2025-01-09 12:05:02

使用正则表达式

  • \b 匹配单词边界,使用它仅匹配整个单词
  • 使用标志 i 执行不区分大小写的匹配

像这样匹配每个单词:

function stopWords($string, $stopwords) {
    foreach ($stopwords as $stopword) {
        $pattern = '/\b' . $stopword . '\b/i';
        if (preg_match($pattern, $string)) {
            return true;
        }
    }
    return false;
}

$stopwords = array('fuc', 'dic', 'pus');

$bad = stopWords('confucius', $stopwords); // true
$bad = stopWords('what the Fuc?', $stopwords); // false

较短的版本,灵感来自于这个问题的答案:确定字符串是否包含一个数组中的一组单词就是使用implode创建一个大表达式:

function stopWords($string, $stopwords) {
    $pattern = '/\b(' . implode('|', $stopwords) . ')\b/i';
    return preg_match($pattern, $string) > 0;
}

Use regular expressions:

  • \b matches a word boundary, use it to match only whole words
  • use flag i to perform case-insensitive matches

Match each word like so:

function stopWords($string, $stopwords) {
    foreach ($stopwords as $stopword) {
        $pattern = '/\b' . $stopword . '\b/i';
        if (preg_match($pattern, $string)) {
            return true;
        }
    }
    return false;
}

$stopwords = array('fuc', 'dic', 'pus');

$bad = stopWords('confucius', $stopwords); // true
$bad = stopWords('what the Fuc?', $stopwords); // false

A shorter version, inspired by an answer to this question: determine if a string contains one of a set of words in an array is to use implode to create one big expression:

function stopWords($string, $stopwords) {
    $pattern = '/\b(' . implode('|', $stopwords) . ')\b/i';
    return preg_match($pattern, $string) > 0;
}
滥情哥ㄟ 2025-01-09 12:05:02
function stopWords($string, $stopwords) {
    $words=explode(' ', $string); //splits the string into words and stores it in an array
    foreach($stopwords as $stopword)//loops through the stop words array
    {
        if(in_array($stopword, $words)) {//if the current stop word exists 
            //in the words contained in $string then exit the function 
            //immediately and return true
            return true;
        }
    }
    //else if none of the stop words were in $string then return false
    return false;
}

我在这里假设 $stopwords 是一个数组。应该是,如果不是的话。

function stopWords($string, $stopwords) {
    $words=explode(' ', $string); //splits the string into words and stores it in an array
    foreach($stopwords as $stopword)//loops through the stop words array
    {
        if(in_array($stopword, $words)) {//if the current stop word exists 
            //in the words contained in $string then exit the function 
            //immediately and return true
            return true;
        }
    }
    //else if none of the stop words were in $string then return false
    return false;
}

I'm assuming here that $stopwords is an array to begin with. It should be if it's not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文