2012-05-03 2 views
0

문의 양식에 잘못된 단어를 필터링하는 데 문제가 있습니다. 단어의 첫 문자를 제외한 모든 메시지를 제거합니다. 도움이 되었습니까? 아래 PHP 기반 문의 양식에 단어 필터링 필터링

단지 정보

<?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']); 

을 받고 이것이 내가 나쁜 단어를 제거하고 옵션 아래 그들에게

$bad_words = array(
'word1' => 'gosh', 
'word2' => 'darn',); 

function filter($matches) { 
global $bad_words; 
$replace = $bad_words[$matches[0]]; 
return !empty($replace) ? $replace : $matches[0]; 
} 

검사 드롭을 대체하기 위해 사용하려고하고 있어요 기능이없는 것입니다 특정 과목의 이메일 전송을 허용하십시오.

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; 
} 

장소 정보 및 전송

$to = '[email protected]'; 
if (!empty($subject) && !empty($msg) && !empty($email) && !empty($username)){ 
if ($error == ''){ 
    mail($to, $subject, $msg, 'From:' . $email); 
} else { 
    print $error; 
} 
} 

?> 
+0

대답이 없지만'empty()'대신'isset()'을 사용해야합니다. 또한'global'을 사용하지 마십시오. – PeeHaa

답변

0

가 배열을 취할 수 있기 때문에 당신은 않는 str_replace를 사용할 수 있습니다. 예를 들어

:

$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); 

결과는 다음과 같습니다 안녕하세요 나의 좋은 친구! 나는 *처럼 느껴진다해도 오늘 당신을 모두 만나서 매우 기쁩니다.

PHP가 이미 많은 유용한 메소드를 제공 할 때 새로운 메소드를 다시 만드는 이유는 무엇입니까? 이것은 전송되는 메시지에서 "나쁜 단어"를 제거하기에 충분해야합니다.