[英]Issue filtering words in contact form PHP based
I having a issue filtering bad words inside a contact form message. 我在过滤联系表单消息中的不良单词时遇到问题。 It strips all the message except the first letter of the word.
它会去除单词的第一个字母以外的所有消息。 Any help?
有什么帮助吗?
below is just getting the info 下面只是获取信息
<?php
$error = '';
if(!empty($_POST['username'])) $username = $_POST['username'];
if(!empty($_POST['email'])) $email = $_POST['email'];
if(!empty($_POST['subject'])) $subject = check($_POST['subject']);
if(!empty($_POST['message'])) $msg = filter($_POST['message']);
this is the function I am trying to use to strip the bad words and replace them 这是我试图用来去除不良单词并替换它们的功能
$bad_words = array(
'word1' => 'gosh',
'word2' => 'darn',);
function filter($matches) {
global $bad_words;
$replace = $bad_words[$matches[0]];
return !empty($replace) ? $replace : $matches[0];
}
checks the drop down options and doesn't allow certain subjects to be emailed. 检查下拉选项,并且不允许通过电子邮件发送某些主题。
function check($str){
global $error;
if ($str == 'Mean Spirited Comment'){
$error = 'You sent a Mean-Spirited Comment';
} else if ($str =='Political Comment'){
$error = 'You sent a Political Comment';
}
return $str;
}
places the info and sends 放置信息并发送
$to = 'email@email.com';
if (!empty($subject) && !empty($msg) && !empty($email) && !empty($username)){
if ($error == ''){
mail($to, $subject, $msg, 'From:' . $email);
} else {
print $error;
}
}
?>
You could use str_replace since it can take array. 您可以使用str_replace,因为它可以使用数组。
For instance: 例如:
$message = "Hello there my good friends! I am very happy to see you all today even though I feel like crap.";
$badWords = array("Crap", "Damnit", "Frack");
echo str_replace($badWords, "*", $message);
Results would be: Hello there my good friends! 结果将是:您好,我的好朋友们! I am very happy to see you all today even though I feel like *.
我很高兴今天见到大家,尽管我感觉很像*。
Why re-invent new methods when PHP already offers plenty of useful ones? 当PHP已经提供了很多有用的方法时,为什么还要重新发明新方法呢? This should be enough to remove "bad words" from messages being sent.
这应该足以从正在发送的消息中删除“坏词”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.