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.
Results 1 to 9 of 9

Thread: Case sensitive

  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Case sensitive

    I have a badword-filter for a guestbook on my site, but I don't want to have to search for every possible comination of upper and lower case letters, so I need it to not be case sensitive, I cant convert the whole string to uppercase and lowercase since it then outputs it to the guestbook,
    Does anyone know what to do?
    I don't see why I should post my code, but if it will be useful then I will post it.

    All help is greatly apreciated

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    PHP Code:
    function filterData($t) {
        
    $r=stripslashes(htmlspecialchars($t,ENT_QUOTES));
        
    $words = array("/sex/i","/love/i","/bad/i","/words/i","/here/i","/damn/i");
        
    $replaceTxt "[removed]";
        foreach(
    $words as $word) {
            
    $r preg_replace($word$replaceTxt$r);
        }
        return 
    $r;


  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    create another var and convert it to upper or lower and test that in stead

    PHP Code:
    $word="rudeword";
    $testword=strtoupper($word);
    if(
    $testword=="RUDEWORD")
    {
    $word="******";


  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, it works great
    I never thought of doing that...

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sidney, that would hardly work... wHaT iF i EnTeREd wOrdS lIKE tHIs?! The preg_replace with the pattern modifier is the better solution -- of course I'm lazy so I'd do it like -
    PHP Code:
    preg_replace("/".$word."/i"$replaceTxt$r); 
    You might want to throw checking for spaces around the word to, some legit words have filtered words in them ...
    PHP Code:
    preg_replace("/\040".$word."\040/i"$replaceTxt$r); 
    Or maybe you could use assertions...
    Omnis mico antequam dominus Spookster!

  • #6
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Or

    PHP Code:
    preg_replace("/\b".$badWord."\b/i","[REMOVED]",$text); 

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    Sidney, that would hardly work... wHaT iF i EnTeREd wOrdS lIKE tHIs?! The preg_replace with the pattern modifier is the better solution -- of course I'm lazy so I'd do it like -
    PHP Code:
    preg_replace("/".$word."/i"$replaceTxt$r); 
    You might want to throw checking for spaces around the word to, some legit words have filtered words in them ...
    PHP Code:
    preg_replace("/\040".$word."\040/i"$replaceTxt$r); 
    Or maybe you could use assertions...
    yes it would work as words like ThInK aGaIn would be converted to
    think again or THINK AGAIN. so the test for banned words wold be in one case what ever was entered and no need to call the regexp engine

    note i must add that i think any form of censorship is a horses arse
    Last edited by sidney; 05-04-2004 at 01:32 AM.

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    a bit off topic but note that for PHP5 we will have str_ireplace for case insensitive replacements without the overhead of regex ... & a long time coming I must say !
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    a bit off topic but note that for PHP5 we will have str_ireplace for case insensitive replacements without the overhead of regex ... & a long time coming I must say !

    Oh kool, well actually what it doesn't do a preg_replace, I just used parts of sydneys idea. What it actually does is it converts the current string to a diffrent string that is all lowercase, and after it searches the new converted to lower case string for and rude words, and if it finds them it doesn't post the other string and automatically bans them


  •  

    Posting Permissions

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