首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从PHP字符串中的数组中找到准确的单词

如何从PHP字符串中的数组中找到准确的单词
EN

Stack Overflow用户
提问于 2020-08-27 03:48:35
回答 1查看 311关注 0票数 1

我在string中搜索array中的一组单词,以便通知用户(如果发现了任何单词)。然而,我得到的结果并不完全匹配。任何关于我如何使它显示精确匹配的想法。我的代码如下所示。

代码语言:javascript
复制
<?php

// Profanity check  
 $profaneReport = "";
 $allContent = "Rice Beans Class stite";
 $profanity_list = "lass tite able";

      $profaneWords = explode( ' ', $profanity_list );

      $wordsFoundInProfaneList = []; // Create words an array

      //search for the words;

      foreach ( $profaneWords as $profane ) {

        if ( stripos( $allContent, $profane ) !== false ) {

          $wordsFoundInProfaneList[ $profane ] = true;

        }

      }


    // check if bad words were found

      if ( $wordsFoundInProfaneList !== 0 ) {

         $profaneReportDesc = "Sorry, your content may contain such words as " . "<strong>" . implode( ", ", array_keys( $wordsFoundInProfaneList )) . '</strong>"';

      } else {

       $profaneReportDesc = "Good: No profanity was found in your content";

      }

     echo $profaneReportDesc;
  

?>

上面的代码返回对不起,您的内容可能包含这样的单词,如lass,tite",当它们与$allContent中的单词不完全匹配时。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-27 05:46:41

为了帮助其他用户寻找类似问题的答案,并在Alex Howansky's comment的基础上添加更多的输入字符串准备,以便更容易地将其转换为一系列单词,您可以这样做:

  1. Remove all标点符号等可能会影响将字符串拆分为单个单词的,并通过将所有非字母数字字符替换为空格(例如,1),确保单词由空格分隔。现在可以识别的是三个单独的单词) comparison
  2. Explode
  3. 将输入字符串和亵渎字符串转换为小写以便更容易地将两个字符串转换为数组(在这里,替换输入字符串中的空格是important!)
  4. Intersect数组以查找两个

共有的单词

您可能也需要考虑从输入字符串中移除数字,具体取决于处理数字的方式。

附有详细注释的完整代码如下:

代码语言:javascript
复制
// Profanity check  
$profaneReport = "";
$profanity_list = "hello TEN test commas";    
$allContent = "Hello, world! This is a senTENce for testing. It has more than TEN words and contains some punctuation,like commas.";

/* Create an array of all words in lowercase (for easier comparison) */
$profaneWords = explode( ' ', strtolower($profanity_list) );

/* Remove everything but a-z (i.e. all punctionation numbers etc.) from the sentence 
   We replace them with spaces, so we can break the sentence into words */
$alpha = preg_replace("/[^a-z0-9]+/", " ", strtolower($allContent));

/* Create an array of the words in the sentence */
$alphawords = explode( ' ', $alpha );

/* get all words that are in both arrays */
$wordsFoundInProfaneList = array_intersect ( $alphawords, $profaneWords);

// check if bad words were found, and display a message 
if ( !empty($wordsFoundInProfaneList)) {
    $profaneReportDesc = "Sorry, your content may contain such words as " . "<strong>" . implode( ", ", $wordsFoundInProfaneList) . '</strong>"';
} else {
    $profaneReportDesc = "Good: No profanity was found in your content";
}
echo $profaneReportDesc;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63608870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档