dongtong2021 2014-09-06 23:02 采纳率: 0%
浏览 59
已采纳

坏词过滤器

Im trying to do a bad words filter with the following function:

function sacarmalaspalabras($texto) {
$palabras = Array(" sexo "," concha ", " pete "," vagina ", " culo ", " anal ", " puta ", " puto    ", " putas ", " putos ", " preservativo ", " preservativos ", " orgasmo ",  " porno ", " pija ", " teta " , " tetas ", " condon ", " masturbar ", " masturbaba ", " poronga ", " concha ", " hentai ", " gay ", " lesbiana ", " orto ", " choto ", " chota ", " marica " ," maricon ", " semen ", " garchar ", "hentai","porn"," porn " );

 $palabrasnew = Array(" s*xo "," c*ncha ", " p*te ", " v*gina ", " cul* ", "an*l ", " p*ta ", " p*to ", " p*tas ", " p*tos ", " preservativ* ", " preservativ*s ", " or**smo ", " p*rno ", " pij* ", " tet* ", " tet*s ", " cond*n ", " mastur*** ", " mast**baba ", " poron** ", " conch* " , " h**tai ", " g*y ", " lesb**na ", " ort* ", " chot* ", " chot* ", " maric* ", " maric*n ", " sem*n ", " garch** ", "h**tai", "p*rn", " p*rn " );

$texto = str_replace($palabras,$palabrasnew,$texto);

return $texto;
}

the problem is that its replacing the word only if its between white spaces lets say i want to filter the word "ass" , if someone says: "that ass, another thing" it will not replace it due to the comma. But also i want to prevent the replacement of words inside other words, lets say im trying to filter the word "sex" but i dont want to filter "sexuality".

how can i use a wildcard on str_replace? so its something like %ass% there might be some words that will fall in the filter, but it will be the minority.

  • 写回答

3条回答 默认 最新

  • douduocuima61392 2014-09-06 23:24
    关注

    Here is the function using preg_replace instead

    function sacarmalaspalabras($texto) {
    $palabras = Array("/\bsexo\b/i", "/\bconcha\b/i", "/\bpete\b/i", "/\bvagina\b/i", "/\bculo\b/i", "/\banal\b/i", "/\bputa\b/i", "/\bputo\b/i", "/\bputas\b/i", "/\bputos\b/i", "/\bpreservativo\b/i", "/\bpreservativos\b/i", "/\borgasmo\b/i", "/\bporno\b/i", "/\bpija\b/i", "/\bteta\b/i", "/\btetas\b/i", "/\bcondon\b/i", "/\bmasturbar\b/i", "/\bmasturbaba\b/i", "/\bporonga\b/i", "/\bconcha\b/i", "/\bhentai\b/i", "/\bgay\b/i", "/\blesbiana\b/i", "/\borto\b/i", "/\bchoto\b/i", "/\bchota\b/i", "/\bmarica\b/i", "/\bmaricon\b/i", "/\bsemen\b/i", "/\bgarchar\b/i", "/\bhentai\b/i", "/\bporn\b/i", "/\bporn\b/i");
    
      $palabrasnew = Array("s*xo", "c*ncha", "p*te", "v*gina", "cul*", "an*l", "p*ta", "p*to", "p*tas", "p*tos", "preservativ*", "preservativ*s", "or**smo", "p*rno", "pij*", "tet*", "tet*s", "cond*n", "mastur***", "mast**baba", "poron**", "conch*", "h**tai", "g*y", "lesb**na", "ort*", "chot*", "chot*", "maric*", "maric*n", "sem*n", "garch**", "h**tai", "p*rn", "p*rn");
    
      $texto = preg_replace($palabras,$palabrasnew,$texto);
    
      return $texto;
    }
    

    You can test it by running a line like this:

    echo sacarmalaspalabras("Sexologist sexo sexu osex");
    

    The output is : Sexologist s*xo sexu osex

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化