如何使用PHP用星号替换注释中的坏词 [英] How to Replace Bad Words in a Comment with Asterisks Using PHP

查看:49
本文介绍了如何使用PHP用星号替换注释中的坏词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


希望所有人都很好.

Hope All Of You are Fine.

我有一个坏词文件,我想用星号替换注释中的坏词.

I have a File in Of Bad Words, I want to Replace Bad Words in a Comments with Asterisks.

坏词网址:

Bad Words URL : https://gist.githubusercontent.com/anonymous/e8e6798137b1ff4836d6ebcf73fef7dc/raw/415dfc8cbab13fa6033fbb4d4ce9eae7a9dbe7cd/Bad_Words.txt

我写了下面的代码行,但是不起作用:(

I wrote Below Lines Of Code But It's not Working :(

请帮助.

<?php
$abusive_words = file_get_contents('Bad_Words.txt');
$abusive_words = explode("\n", $abusive_words);
$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";
?>

推荐答案

在从文本文件中检索数组元素($ abusive_words)的过程中,有时可能会附加空格.由于这些空格,str_ireplace()可能无法获得完美匹配.因此,在继续进行下一步之前尤其要进行比较,始终最好修剪数组元素.

There might be cases where whitespace is appended to the array element($abusive_words) while retrieving it from the text file. str_ireplace() might not be able to get perfect match due to these whitespaces. So its always better to trim the array elements before proceeding further especially for comparison.

array_map()和修剪是您所需要的.

array_map() and trim is what you need.

$ abusive_words = array_map('trim',$ abusive_words);

$abusive_words = array_map('trim', $abusive_words);

在将数组传递给str_ireplace()之前执行此操作

Do this before passing array to str_ireplace()

这篇关于如何使用PHP用星号替换注释中的坏词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆