SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote a censorship function for my site but I want to make it work better. Currently work but it will censor out the wrong things sometimes.

    The way it works it replaces the letters with a symbol. I used "-".

    For example let's say I want to censor out the word "cat", the function will censor any occurance of the word cat showing the user "---".

    The way I call the function is as follows:

    $text = censor_text($text);

    However if the word is catalog. It will spit out "---alog".

    So I want to fine tune it down to only censor the EXACT word. Any other additions you can come up with would be great.

    ----------------------------

    function censor_text($text) {

    $censor_list = "cat|dog|bird";

    $censor_array = explode("|",$censor_list);

    $number_of_words = count($censor_array);

    for ($counter = 0; $counter < $number_of_words; $counter++) {

    $censor_word = $censor_array[$counter];

    $censor_letter_count = strlen($censor_word);

    for ($letter_count = 0; $letter_count < $censor_letter_count; $letter_count++) {

    $replace_string .= "-";

    }

    $text = eregi_replace($censor_word,$replace_string,$text);

    $replace_string = "";

    }

    return $text;

    }
    <///////~

    www.sohh.com

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay I took a slightly different approach:

    Code:
    <?
    function censor_text($text) { 
    	$data = explode(" ", $text);
    	$censor_array = array("cat","dog","bird"); 
    	for($i=0;$i<count($data);$i++) {
    		if (in_array(strtolower($data[$i]), $censor_array)) {
    			for($j=0;$j<strlen($data[$i]);$j++) {
    				$tmp .= "-";
    				}
    			$data[$i] = $tmp;
    			unset($tmp);
    			}
    		}	
    	return implode(" ", $data); 
    	}
    
    print censor_text("This Cat lives in a catalog with his pal the Dog");
    	
    	
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lemme ask you this, if you wanted to pass a list of words to the array how would you do it? For example:

    $censorwords = "cat,dog,bird";

    Just a question...
    <///////~

    www.sohh.com

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would actually pass the array to the function like this:

    Code:
    function censor_text($text, $censor_array) { 
    	$data = explode(" ", $text);
    	for($i=0;$i<count($data);$i++) {
    		if (in_array(strtolower($data[$i]), $censor_array)) {
    			for($j=0;$j<strlen($data[$i]);$j++) {
    				$tmp .= "-";
    				}
    			$data[$i] = $tmp;
    			unset($tmp);
    			}
    		}	
    	return implode(" ", $data); 
    	}
    
    print censor_text("This Cat lives in a catalog with his pal the Dog", array("cat","dog","bird"));
    	
    	
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to put it in an application where the person would be able to change the censorship words. Like in the config file, they could set the censorwords_variable.

    I actually want you to check out the app I wrote. It's my second PHP app and I really respect the quality of advice I've seen you post on this site. I'll send you the links via PM.
    <///////~

    www.sohh.com

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then the easiest way to do that would be to create the array in the config file and simply globalize it inside the function like so.
    ===configfile=======
    $censor_array = array("cat","dog","bird");






    ====Script file======
    include("configfile");

    function censor_text($text) {
    global $censor_array;
    $data = explode(" ", $text);
    for($i=0;$i<count($data);$i++) {
    if (in_array(strtolower($data[$i]), $censor_array)) {
    for($j=0;$j<strlen($data[$i]);$j++) {
    $tmp .= "-";
    }
    $data[$i] = $tmp;
    unset($tmp);
    }
    }
    return implode(" ", $data);
    }

    print censor_text("This Cat lives in a catalog with his pal the Dog");
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once again FDP does his thing. Works rather well.

    http://www.sohh.com/search/template.php?categoryID=1

    If you look at the description for Hieroglyphics.com, you'll see it censors a word out. One drawback is that it won't catch words that have a symbol attached. Like 'cat.' will get through while 'cat' will get censored.

    Oh well...no biggie.
    <///////~

    www.sohh.com

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This ought to ssolve that problem, just add to the $punc_array to add more puncuation you want left out.

    Code:
    <?
    $censor_array = array("cat", "dog", "bee");
    $punc_array = array(".", ",", "?");
    function censor_text($text) { 
    	global $censor_array, $punc_array;
    	
    	$data = explode(" ", $text);
    	for($i=0;$i<count($data);$i++) {
    		if (in_array(strtolower(ereg_replace("[\.,\?]", "", $data[$i])), $censor_array)) {
    
    			for($j=0;$j<strlen($data[$i]);$j++) {
    				if(in_array(substr($data[$i],$j,1), $punc_array)) {
    					$tmp .= substr($data[$i],$j,1);
    					}
    				else {
    					$tmp .= "-";
    					}
    				}
    			$data[$i] = $tmp;
    			unset($tmp);
    			}
    		}	
    	return implode(" ", $data); 
    	}
    
    print censor_text("This Cat lives in a catalog with his pal the Dog.");
    	
    	
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <///////~

    www.sohh.com

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh.. Where is the error? Also did you add that punctuation mark to the $punc_array array?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this:

    $punc_array = array(".", ",", "?", "!");

    Check out the description of Hieroglyphics.com (first link under the links section.)
    <///////~

    www.sohh.com

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah sorry since I didn't automate the regex you need to add the same punctuation to this line like I did here

    if (in_array(strtolower(ereg_replace("[\.,\?\!]", "", $data[$i])), $censor_array)) {
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup, yup

    ereg_replace("[\.,\?\!]"

    I need to read up on this function. I get it but I don't.

    Thanks!
    <///////~

    www.sohh.com

  14. #14
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well what it means is if the current word in the string converted to lowercase(strtolower() function) minus and puncutation(the ereg() function) is in the array $punc_array then skip that character in the string and replace everything else with a "-". Hope that clears it a up a bit.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •