Log In

Email:
Password:
Keep me logged in

Forgot Password

Email:




Social Media
     
CategoriesCSS (5)
jQuery (73)
PHP (30)
.htaccess (4)
Sponsored Links


Censor Words
PHP jQuery Javascript
Demo | Browse Category | Friday, February 10, 2012 by




PHP:
<?php

$list 
= array('ass''bitch''damn''fuck''shit'); //ARRAY OF WORDS TO CENSOR
$str 'What the Fuck is that? You mothafuckin bitch!!!'//STRING TO SEARCH THROUGH

//REPLACES EVERY WORD IN 'list' WITH THE FIRST LETTER AND ASTERICKS
for($i=0$i<count($list); $i++){
    
$c $list[$i]; //WORD IN THE ARRAY
    
$firstl substr($c01);    //FIRST LETTER
    
$strlen strlen($c); //NUMBER OF CHARACTER OF EACH WORD
    
$astr str_repeat('*'$strlen-1); //REPLACES THE REST OF WORD WITH ASTERICKS
    
$censored $firstl.$astr//CENSORED WORD Ex: (f***)
    //USED TO PREVENT a (|ass|bitch) IN REGEX
    
if($i==0){
    
$d .= $c;
    } else {
    
$d .= '|'.$c;
    }
    
$str eregi_replace('('.$d.')'$censored$str); //CENSORING PROCESS HERE
}

echo 
$str;

?>
jQuery/Javascript:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function censor(str){ //STRING VARIABLE FROM <input> TAG
var astericks = '****'; //ASTERICKS
var profanity = /^(S)*(ass|bitch|damn|fuck|shit)+(S)*$/gi; //ARRAY OF WORDS TO CENSOR
var s = ''; //BLANK VARIABLE UNTIL THE SECOND 'FOR LOOP'

var w = str.split(" "); //SPLITS EACH WORDS BY SPACES
var words = w.length; //THE NUMBER OF SPLIT
for(i=0; i<str.length; i++){
if(str.substr(i, 2)=="  "){ //BY DEFAULT, > 1 SPACES COUNT AS A WORD, THIS PREVENTS THE EXTRA
words--; //DECREASES THE NUMBER OF WORDS BY 1 IF THERE ARE > 1 SPACES IN A ROW
}
}

for(i=0; i<words; i++){
if(w[i].match(profanity)){ //CHECKS TO SEE IF WORDS MATCH ANY OF THE LIST
s += w[i][0]+astericks+' '; //IF SO, ADDS THE ASTERICKS TO EACH WORD FROM THE LIST
} else {
s += w[i]+' '; //JUST ADDS THE OTHER WORDS IN THE STRING
}
}

$("#censored").html(s); //DISPLAYED IN HTML
}
</script>
</head>
<body>
<input onKeyUp="censor(this.value)"><br>
<span id="censored"></span>
</body>
</html>
Views: 109 Likes: 1 Dislikes: 0






wcet2011
@TheNut the (jQuery/Javascript) error is fixed.

Tuesday, April 24, 2012 at 7:48:42 PM
TheNut
One more thing. It will change exactly those variables, so other, normal word which contains bad words will be also changed ie class => cla****

Tuesday, April 24, 2012 at 12:43:02 AM
TheNut
Hi, I've tried this script (jQuery/Javascript) and it looks very nice, but I have 2 problems:
1. Letter size matter, so Word, WORD and word aren't the same
2. It replace every word only once, if user put 'word word word' we will get 'w**** word word'
How to solve those problems?

Tuesday, April 24, 2012 at 12:40:22 AM