Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're all trying wayyyy to hard to do something and using crazy functions and stuff. Some code doesn't even work. I developed this in JavaScript then converted it to PHP.
    function filter($input,$words,$to) {
    if (file_exists($words)) {$words=explode("|",str_ireplace(array("\r", "\n"),"|",file_get_contents($words)));};
    for ($i=0;$i<=count($words) - 1;$i++)
    {
    $cv="";for ($g=1;$g<=strlen($words[$i]);$g++) {$cv=$cv . $to;};
    $input=str_ireplace($words[$i],$cv,$input);
    };
    return $input;
    };

    Examples

    words.txt:
    hey
    great

    From File:
    $stuff="Hey, how's it going? Yeah same, been great";
    echo filter($stuff,"words.txt","*");

    Output: ***, how's it going? Yeah same, been *****

    From Array:
    $stuff="Hey I like words more than evenmore. No idea what that means";
    $words=array("words","more","evenmore");
    echo filter($stuff,$words,"*");
    Output: Hey I like ***** **** than ********. No idea what that means

    Have fun.

  2. #17
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Wow, that is some crazy looking code, with the explode(), str_ireplace() with no letters, and file_get_contents() all doing the work of file():
    PHP Code:
    function filter$input$words$to '*' )
    {

        if ( 
    is_file$words ) )
        {
            
    $words file$wordsFILE_IGNORE_NEW_LINES );
        }

        foreach ( 
    $words as $word )
        {
            
    $input str_ireplace$wordstr_repeat$tostrlen$word ) ), $input );
        }

        return 
    $input;


    Last edited by kbluhm; 08-21-2011 at 03:31 PM.

  3. #18
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My solution

    This is the best solution I found so far for a bad word censor.
    Code:
            /**
    	 * Returns true if a bad word is detected in the string
    	 * @param String $str The string to check for bad words
    	 * @return boolean
    	 */
    	public function has_bad_words($str) {
    		$badwords = file_get_contents('badwords.txt');
    		$badwords = explode("\n", $badwords);
    		$words = str_word_count($str, 1);
    		foreach($words as $word) {
    			foreach($badwords as $bw) {
    				if (strtolower($word) == strtolower($bw)) {return true;}
    			}
    		}
    		return false;
    	}//end function has

  4. #19
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    That doesn't replace bad words, it only tells you if the string contains bad words. And your evaluation of "best" is pretty subjective.


 

Posting Permissions

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