我计划将这个坏词列表 ( https://github.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/blob/master/en ) 放入我的网络服务器上的 .txt 文件中。我如何通过此 javascript 对照网络服务器上名为“blacklist.txt”的 .txt 文件检查变量“userNickName”。
(这是我想要实现坏词检查的代码,我该怎么做?)
if (wsIsOpen() && null != userNickName) {
var msg = prepareData(1 + 2 * userNickName.length);
msg.setUint8(0, 0);
for (var i = 0; i < userNickName.length; ++i) msg.setUint16(1 + 2 * i, userNickName.charCodeAt(i), true);
wsSend(msg)
最佳答案
创建一个 JS 数组,例如 badWords = ["boob","boobs","..."];
这可以通过 Get file contents in java script 来完成和 How do I split a string, breaking at a particular character?
或者直接在服务器端使用 PHP。
$badWords = file_get_contents('https://raw.githubusercontent.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/master/en');
$badWords = explode("\n", $badWords);
echo 'badWords = ["'.implode('","', $badWords).'"];';
<小时/>
之后,How to find if an array contains a specific string in JavaScript/jQuery? (非 JQuery 的第二个答案)
关于javascript - 在我的 html 输入中实现坏词过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35105058/