如何使用PHP替换星号评论中的坏词

时间:2017-03-31 06:56:25

标签: php arrays


希望你们都很好。

我有一个糟糕的文件,我想用星号替换评论中的坏词。

坏词网址:https://gist.githubusercontent.com/anonymous/e8e6798137b1ff4836d6ebcf73fef7dc/raw/415dfc8cbab13fa6033fbb4d4ce9eae7a9dbe7cd/Bad_Words.txt

我写了下面的代码行,但它没有工作:(

请帮助。

public class Main
{
    public static void main( String[] args )
    {
        String rawData = "John\n\nDavid\nGeorge\nFrank\nTom";

        Pattern pattern = Pattern.compile("\\n");  

        System.out.println( lastN( pattern.splitAsStream( rawData ),4));
        System.out.println( lastN( pattern.splitAsStream( rawData ),40));
    }

    public static <T> List<T> lastN( Stream<T> stream, int n )
    {
        Deque<T> result = new ArrayDeque<>( n );
        stream.forEachOrdered( x -> {
            if ( result.size() == n )
            {
                result.pop();
            }
            result.add( x );
        } );
        return new ArrayList<>( result );
    }
}

4 个答案:

答案 0 :(得分:1)

在从文本文件中检索空格时,可能会出现将空格附加到数组元素($ abusive_words)的情况。由于这些空格,str_ireplace()可能无法完美匹配。因此,在进一步进行特别是比较之前,最好修剪数组元素。

array_map()和trim是你需要的。

$ abusive_words = array_map(&#39; trim&#39;,$ abusive_words);

在将数组传递给str_ireplace()

之前执行此操作

答案 1 :(得分:1)

这里我分享了一个例子,我们只需要用字符设置替换,它将替换字符串的字符。

代表。 Bitch将替换为*****(5星),而废话将显示****(4次开始)。

$abusive_words = ['bitch', 'crap', 'bastard'];
$input_string = 'Catch that bastard, Idiot .... !!';
$replaceWith = '*';

$replace = array();
$replace['a'] = '(a|a\.|a\-|4|@|Á|á|À|Â|à|Â|â|Ä|ä|Ã|ã|Å|å|α|Δ|Λ|λ)';
$replace['b'] = '(b|b\.|b\-|8|\|3|ß|Β|β)';
$replace['c'] = '(c|c\.|c\-|Ç|ç|¢|€|<|\(|{|©)';
$replace['d'] = '(d|d\.|d\-|&part;|\|\)|Þ|þ|Ð|ð)';
$replace['e'] = '(e|e\.|e\-|3|€|È|è|É|é|Ê|ê|∑)';
$replace['f'] = '(f|f\.|f\-|ƒ)';
$replace['g'] = '(g|g\.|g\-|6|9)';
$replace['h'] = '(h|h\.|h\-|Η)';
$replace['i'] = '(i|i\.|i\-|!|\||\]\[|]|1|∫|Ì|Í|Î|Ï|ì|í|î|ï)';
$replace['j'] = '(j|j\.|j\-)';
$replace['k'] = '(k|k\.|k\-|Κ|κ)';
$replace['l'] = '(l|1\.|l\-|!|\||\]\[|]|£|∫|Ì|Í|Î|Ï)';
$replace['m'] = '(m|m\.|m\-)';
$replace['n'] = '(n|n\.|n\-|η|Ν|Π)';
$replace['o'] = '(o|o\.|o\-|0|Ο|ο|Φ|¤|°|ø)';
$replace['p'] = '(p|p\.|p\-|ρ|Ρ|¶|þ)';
$replace['q'] = '(q|q\.|q\-)';
$replace['r'] = '(r|r\.|r\-|®)';
$replace['s'] = '(s|s\.|s\-|5|\$|§)';
$replace['t'] = '(t|t\.|t\-|Τ|τ)';
$replace['u'] = '(u|u\.|u\-|υ|µ)';
$replace['v'] = '(v|v\.|v\-|υ|ν)';
$replace['w'] = '(w|w\.|w\-|ω|ψ|Ψ)';
$replace['x'] = '(x|x\.|x\-|Χ|χ)';
$replace['y'] = '(y|y\.|y\-|¥|γ|ÿ|ý|Ÿ|Ý)';
$replace['z'] = '(z|z\.|z\-|Ζ)';

$replacement = array();
$whiteListCount = count($abusive_words);

for ($x = 0; $x < $whiteListCount; $x++) {
    $replacement[$x] = str_repeat($replaceWith, strlen($abusive_words[$x]));
    $abusive_words[$x] = '/' . str_ireplace(array_keys($replace), array_values($replace), $abusive_words[$x]) . '/i';
}
echo preg_replace($abusive_words, $replacement, $input_string);

您可以在单独的函数中移动主逻辑束代码,并在任何要从任何长度的内容中替换坏词的文件中使用。

答案 2 :(得分:0)

<?php
   $abusive_words = file_get_contents('Bad_Words.txt');
   $abusive_words = explode("\n", $abusive_words); // here you got an array of strings
   $input_string = 'Catch that bastard, Idiot .... !!';

   $fixed=$input_string;
   foreach ($abusive_words as $ab_word) // then you need to use loop to get each element of array
      $fixed = str_ireplace($ab_word,'*****',$fixed); //replacing any array string to *****

   echo "Input String <br> $input_string <br><br><hr><br>Input String  Fixed <br> $fixed";
 ?>

答案 3 :(得分:0)

检查一下,

$abusive_words = ['bastard','idiot'];
$input_string = 'Catch that bastard, Idiot .... !!';
$fixed = str_ireplace($abusive_words,'*****',$input_string);
echo "Input String <br> $input_string <br><br><hr><br>Input String Fixed <br> $fixed";

这是工作code

我相信你没有得到abusive_words变量。

请一次print_r该变量并检查您是否在其中获取值。

休息一切正常。