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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line