如何过滤字符串中的多个限制词?
How to filter multiple restricted words in a string?
在我的网站上,我有一个状态更新表单,用户可以填写该表单以更新他们的状态,最多 160 个字符。到目前为止,在我的表单上有一些限制,例如:“用户不能 post >160 个字符,如果他添加 >160 个字符,则会向他显示一条警告消息。”这一切都对我有用。
Now I want to add a restriction on the user input, meaning that if a user enters restricted words, then the post will not be submitted and the user will see an error message.
限制字词:Facebook、Twitter、Whatsapp、Mxit , Qeep.
到目前为止,我只能在我的函数中添加一个词,我想将以上所有词都添加到它的帮助中!谢谢
<?php
$txt = $_POST['msg'];
if (strlen($txt) > 160) {
echo "Your post contains more then 160 chrecters";
$checking = substr($txt, 160);
echo "<del style='color:red;'>$checking</del>";
}
if (preg_match("/Facebook/", $txt)) {
echo "the post contains words restricted!";
}
//else send data to the database
由于字符串很短:
<?php
// Note that this will remove newlines!
$message = filter_input(INPUT_POST, "msg", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_ENCODE_AMP);
// Note the usage of a mutli-byte function.
if (mb_strlen($message) > 160) {
exit("Your message contains more then 160 characters.");
}
// Array containing the all lower-cased words which are restricted.
$restricted_words = array("facebook", "twitter");
// Lowercase the message for our search (again multi-byte).
$words = mb_strtolower($message);
// Create an array by splitting the words at the grammatically correct word
// delimiter character (a space).
$words = explode(" ", $words);
// Flip the array, so we can directly check with isset() for existence.
$words = array_flip($words);
// Now go through all restricted words and see if they are part of the message.
foreach ($restricted_words as $delta => $restricted_word) {
if (isset($words[$restricted_word])) {
exit("Your message contains a restricted word.");
}
}
我发现你的整个方法有一个问题,因为你只检查了完美输入的单词。过去的许多项目都试图对他们的用户和类似的东西强加亵渎过滤器。这就是为什么您会经常看到人们 post 使用 fu@#
或 dafuq
而不是 fuck
或 what the fuck
。您的用户可能只是求助于类似的东西 post FB
而不是 Facebook
。只是重新考虑一下这样的单词过滤器是否真的有必要。如果是,请考虑使用 Levenshtein distance 来检查单词是否相似(这将是一项昂贵的操作,并且可能会产生误报)。
最后一点,您正在搜索的正则表达式:
<?php
preg_match("/(Facebook|Twitter)/i", $message, $matches);
括号创建一个组,管道用于分隔我们要匹配的各种备选词。最后但同样重要的是 i
修饰符用于使整个事物不区分大小写。 (可选的)第三个参数将包含匹配项,这样您就可以告诉用户在邮件中找到了哪些受限制的词。
<?php
$restricted_words = array("facebook", "twitter", "google plus"); //add restricted word or character in array
$replace = array(''); // add here word or character with whom you want to replace, i added blank because i want to replace with blank
$message = str_replace($restricted, $replace, $_POST['message']);
?>
在我的网站上,我有一个状态更新表单,用户可以填写该表单以更新他们的状态,最多 160 个字符。到目前为止,在我的表单上有一些限制,例如:“用户不能 post >160 个字符,如果他添加 >160 个字符,则会向他显示一条警告消息。”这一切都对我有用。
Now I want to add a restriction on the user input, meaning that if a user enters restricted words, then the post will not be submitted and the user will see an error message.
限制字词:Facebook、Twitter、Whatsapp、Mxit , Qeep.
到目前为止,我只能在我的函数中添加一个词,我想将以上所有词都添加到它的帮助中!谢谢
<?php
$txt = $_POST['msg'];
if (strlen($txt) > 160) {
echo "Your post contains more then 160 chrecters";
$checking = substr($txt, 160);
echo "<del style='color:red;'>$checking</del>";
}
if (preg_match("/Facebook/", $txt)) {
echo "the post contains words restricted!";
}
//else send data to the database
由于字符串很短:
<?php
// Note that this will remove newlines!
$message = filter_input(INPUT_POST, "msg", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_ENCODE_AMP);
// Note the usage of a mutli-byte function.
if (mb_strlen($message) > 160) {
exit("Your message contains more then 160 characters.");
}
// Array containing the all lower-cased words which are restricted.
$restricted_words = array("facebook", "twitter");
// Lowercase the message for our search (again multi-byte).
$words = mb_strtolower($message);
// Create an array by splitting the words at the grammatically correct word
// delimiter character (a space).
$words = explode(" ", $words);
// Flip the array, so we can directly check with isset() for existence.
$words = array_flip($words);
// Now go through all restricted words and see if they are part of the message.
foreach ($restricted_words as $delta => $restricted_word) {
if (isset($words[$restricted_word])) {
exit("Your message contains a restricted word.");
}
}
我发现你的整个方法有一个问题,因为你只检查了完美输入的单词。过去的许多项目都试图对他们的用户和类似的东西强加亵渎过滤器。这就是为什么您会经常看到人们 post 使用 fu@#
或 dafuq
而不是 fuck
或 what the fuck
。您的用户可能只是求助于类似的东西 post FB
而不是 Facebook
。只是重新考虑一下这样的单词过滤器是否真的有必要。如果是,请考虑使用 Levenshtein distance 来检查单词是否相似(这将是一项昂贵的操作,并且可能会产生误报)。
最后一点,您正在搜索的正则表达式:
<?php
preg_match("/(Facebook|Twitter)/i", $message, $matches);
括号创建一个组,管道用于分隔我们要匹配的各种备选词。最后但同样重要的是 i
修饰符用于使整个事物不区分大小写。 (可选的)第三个参数将包含匹配项,这样您就可以告诉用户在邮件中找到了哪些受限制的词。
<?php
$restricted_words = array("facebook", "twitter", "google plus"); //add restricted word or character in array
$replace = array(''); // add here word or character with whom you want to replace, i added blank because i want to replace with blank
$message = str_replace($restricted, $replace, $_POST['message']);
?>