【问题标题】:compare "array of words" against textarea with php用 php 将“单词数组”与 textarea 进行比较
【发布时间】:2010-08-17 17:33:31
【问题描述】:

我昨天问了这个问题:Compare array of words to a textarea input with javascript

现在我想用 php 做同样的事情...

有什么简单的代码吗?

谢谢

更新:我想根据数组测试 textarea 输入,如果找到匹配的坏词,die();

谢谢

【问题讨论】:

  • 执行 javascript 问题中建议的完全相同的操作。学会理解原则,而不仅仅是复制粘贴
  • 如果您发布完整的问题而不是简单地发布链接会更有帮助。另外,您是要过滤掉“坏”词还是要测试是否有任何坏词然后验证失败?如果验证失败,您是否需要告诉他们哪些单词失败了?
  • ok sorry Noah,我想测试一下是否有不好的词...然后失败或“die()”,因为它在 php 中被调用。
  • 请更新您的问题正文。虽然我很欣赏您的回复,但如果每个人都可以直接看到对您问题的编辑,而不是必须通过 cmets 进行搜索,这将更有帮助。

标签: php javascript html


【解决方案1】:

你可以试试这样的:

$bad_words = array('ring','sarah','chuck');

$intersect = array_intersect(explode(' ', strtolower($_POST['textarea'])), $bad_words);

if(count($intersect)) die('You should wash your mouth out with soap!');

array_intersect 将比较两个不同的单词数组并返回两个数组中存在的所有值。因此,如果 count($intersect) 不是 0(在这种情况下被评估为 false),那么您可以存在脚本并输出错误。

【讨论】:

    【解决方案2】:
    $bad_words = array('first','second','third');
    
    $posted = str_ireplace($bad_words, '****', $posted);
    

    这将用“****”替换坏词

    编辑:

    检查 bad_words 中的任何单词是否存在于字符串中:

    foreach( $bad_words as $bad ){
     if( stristr($posted, $bad) !== FALSE )
     {
      $contains_bad_words = TRUE;
     }    
    }
    

    【讨论】:

    • 实际上我正在寻找一个 if 语句,如果发现坏词,我会取消整个操作。非常感谢
    • 另外,有什么方法可以让这个案例不敏感?
    • 我的意思是当数组中只有“bad”时,如果“Bad”“BAD”“bad”进行匹配...
    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多