SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with direty word filter

    I'm trying to code a dirty word filter function, I've finished it yet, but it doens't work, and I really don't know why, here's the code:
    PHP Code:
    <?
    function dwfilter($msg){
        
    /* dirty word filter */

        
    if(is_file("./include/dw.inc")){

            
    $msg strtolower($msg);
            
            
    $dw file("./include/dw.inc");
            
    $dw_replace "***";
            
    $count count($dw);
            for(
    $i 0$i $count$i++){
                
    $msg str_replace($dw,$dw_replace,$msg);
            }
            return 
    $msg;
        }

        else{
            return 
    $msg;
        }
    }
    ?>
    and this is dw.inc:
    PHP Code:
    dwnr1
    dwnr2
    dwnr3
    etc
    ... 
    This function returns the original dirty word, so it doesn't replace it with "***"...

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would tend to suggest that the condition in your first if statement is returning false. Try this code - it will give you an error message if that is the case:
    PHP Code:
    <?
    function dwfilter($msg){
        
    /* dirty word filter */

        
    if(is_file("./include/dw.inc")){

            
    $msg strtolower($msg);
            
            
    $dw file("./include/dw.inc");
            
    $dw_replace "***";
            
    $count count($dw);
            for(
    $i 0$i $count$i++){
                
    $msg str_replace($dw,$dw_replace,$msg);
            }
            return 
    $msg;
        }

        else{
            
    $msg .= " - For some reason the is_file() test failed.";
            return 
    $msg;
        }
    }
    ?>
    Try that - if the is_file bit is failing (meaning it can't find the file with the bad word list in it) you'll get the error message tagged on to the $msg variable that was returned from the function.

  3. #3
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same problem...

    The returned result of the function is the original (bad) word...

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok Sean, thanks, but that's not really a solution, I want to know what's wrong with my function...

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you try Skunk's revised version??

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason its not working is because each element of the array $dw has a newline character in it. So with a simple fix you can make it work using chop()

    PHP Code:
    function dwfilter($msg){
        
    /* dirty word filter */

        
    if(is_file("./dw.inc")){

            
    $msg strtolower($msg);
            
            
    $dw file("./dw.inc");
            
    $dw_replace "***";
            
    $count count($dw);
            for(
    $i 0$i $count$i++){
                
    $dw[$i] = chop($dw[$i]);
                
    $msg str_replace($dw[$i],$dw_replace,$msg);
            }
            return 
    $msg;
        }

        else{
            return 
    $msg;
        }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you freddy!
    That works fine!

  9. #9
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah jeez, I didn't even notice the lack of [$i] for the initial function.

  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)
    That was only part of the problem as str_replace can take arrays as args, the main problem was the newline.
    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
  •