2011-01-29 6 views
0

모욕적 인 단어가 포함 된 배열을 만들려고하는데 누군가 배열 안에있는 단어를 제출하려고하면 오류가 발생합니다.PHP에서 특정 단어를 검열합니까?

몇 번 시도했지만 실패했습니다!

누구든지 나를 도와 줄 수 있습니까? :)

$censor_ary = array('word1', 'word2', 'word3'); 

    foreach ($censor_ary as $censor) 
    { 
     $word = $censor;  
    } 

if ($_POST['mesazhi'] == $word) 
     { 
      echo '<span>P&euml;rdorimi i fjal&euml;ve fyese nuk &euml;sht&euml; e mir&euml;seardhur</span>'; 
     } 
+4

올바르게 수행하는 것은 매우 어려운 작업입니다. 좋은 읽을 거리 : http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-ide-or-incredibly-intercoursing-bad-idea.html –

+1

시도해 본 적이 있으면 코드가 있으면 솔루션에 가까울 수 있습니다. – greg0ire

+0

"몇 번 시도했지만 실패했습니다!"... 실패한 이유는 무엇입니까? 배열 생성? 잠재적 인 모욕적 인 단어를 귀하의 배열과 비교 하시겠습니까? – chahuistle

답변

1

단순 foreach 문을 사용하여 배열을 반복하고 단어가 제출 $_POST 변수에 포함되어있는 경우 (나는 가정하고)이 같은

또는 뭔가를 확인하는 preg_match를 사용

를 여기
$arr = array('word1','word2'); 

foreach ($arr as $word) 
{ 
if (preg_match("$word",$data)) 
{ 
//error here 
} 
} 
+0

'$ arr = ('word1', 'word2'); '그래, 새로운 구문? ;-) – arnaud576875

+0

아, 그게 대부분 펄 스크립트 문법이 될거야. :) –

2

가 있습니다 :

$words = array('word1', 'word2', 'word3', '...'); 
$re_words = array(); 
foreach($words as $word) $re_words[] = preg_quote($word, '#'); 

if (preg_match('#\b(' . implode('|', $re_words) . ')\b#i', $post, $word) { 
    // error, the $post contains the word $word[0] 
} 

$ words 배열에 나열된 단어를 감지합니다.

3
$badWords = array(
    'bad' => '***', 
    'badly' => '***'); 

strtr("This is a bad sentence", $badWords); // This is a *** sentence 

잘못된 단어와 삭제 된 버전 (또는 별표 **)이 포함 된 배열을 만들 수 있습니다. 그런 다음 strtr()을 필터링에 사용할 수 있습니다.

0

내 웹 사이트에서이 코드를 사용해보십시오. 여기서 변수라는 모든 변수를 변수로 바꿉니다. 비속어가 포함 된 CSV 파일이 필요합니다. 이 코드는 맹세 한 단어와 예를 들어 Scunthorpe와 같은 맹목적 인 단어가 포함 된 구별 된 단어를 구별 할 수 있습니다. 또한 단어를 적절한 수의 별으로 대체하고 모든 공통 접미사를 인식합니다. 실행하는 데 시간이 오래 걸리지 만 가양 성의 위험을 크게 줄일 수 있습니다.

//inport profanities csv and list suffixes 
$profanities=explode(",", file_get_contents('NAME OF YOUR CSV FILE GOES HERE')); 

$suffixes=array('','s','es','e','ed','ing','ted','ting','y','ty','d','head','bag','hole','wit','tard','er','ter','en','ten','est','test','able','ible','ful','full'); 

//get text input 
$sanitize_text=$YOUR VARIABLE HERE; 

//combine profanities and sufixes 
foreach($profanities as $profanity) 
{ 
foreach($suffixes as $suffix) 
{ 
$sanitize_terms=$profanity; 
$sanitize_terms.=$suffix; 
$word=$sanitize_terms; 

$match_count=preg_match_all('/'.$word.'/i', $YOUR VARIABLE HERE, $matches); 
for($i=0; $i<$match_count; $i++) 
{ 
$bwstr=trim($matches[0][$i]); 
$sanitize_text=preg_replace('/\b'.$bwstr.'\b/', str_repeat("*", strlen($bwstr)), $sanitize_text); 
} 
} 
} 


$YOUR VARIABLE HERE=$sanitize_text; 
관련 문제