【问题标题】:Preg Match circumflex with ^ in phpPreg 在 php 中将抑扬符与 ^ 匹配
【发布时间】:2023-03-28 11:17:01
【问题描述】:

我知道我会得到很多 asinine cmets,但无论我做什么我都无法弄清楚。我这里有个函数

$filter = mysql_query("SELECT * FROM `filter`");
    $fil = mysql_fetch_array($filter);
    $bad = $fil['filter'];

$bword = explode(",", $bad);
    function wordfilter($output,$bword){
    $badWords = $bword;
$matchFound = preg_match_all("/(" . implode($badWords,"|") . ")/i",$output,$matches);

if ($matchFound) {
  $words = array_unique($matches[0]);
  foreach($words as $word) {
    $output = preg_replace("/$word/","*****",$output);
  }
}
return $output;
    } 

我知道坏词过滤器不受欢迎,但我的客户要求这样做。

现在我在数据库中有一个列表,这里有一些条目。

^ass$,^asses$,^asshopper,^cock$,^coon,^cracker$,^cum$,^dick$,^fap$,^heeb$,^hell$,^homo$,^humping,^jap$,^mick$,^muff$,^paki$,^phap$,^poon$,^spic$,^tard$,^tit$,^tits$,^twat$,^vag$,ass-hat,ass-pirate,assbag

如您所见,我对某些单词使用了抑扬符和美元符号。

我遇到的问题是前三个单词以ass 开头,即使我写了glassesgrasshoppers 之类的东西,它也会挡住这个词,但是前三个之后的一切都很好,我试过了在这些之前添加 3 个条目以防万一这是问题所在,但不幸的是不是。

我写这个有什么问题吗?

【问题讨论】:

  • 您应该为此考虑开源解决方案,而不是发明自己的机制。
  • @SalmanA 我为此尝试了很多开源脚本,但没有一个能完全满足我的需要
  • 尝试将您的正则表达式更改为'/\b('.implode("|",$badwords).')\b/i' 并删除数据库中的^$
  • @Passerby 这也不起作用,它只是开始弄乱我在数据库中的其他单词,其中大约有 150 多个单词。
  • @kira423 然后我对你的$output 的样子很感兴趣。 ^$ 表示“字符串/行的开头”和“字符串/行的结尾”。这就是为什么您不应该直接将它们添加到您的数据库中 - 您可能希望 "bad ass boy" 被过滤,而 ^ass$ 不会这样做,但 \bass\b 应该这样做。

标签: php mysql function preg-match-all


【解决方案1】:

从评论扩展:

尝试使用\b检测单词:

$matchFound = preg_match_all('/\b('.implode($badWords,"|").')\b/i',$output,$matches);

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多