SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: str_replace()

  1. #1
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace()

    Hey everyone

    I am writing a bad word filter. It takes the user text, runs it through the filter, then dumps it into a DB.

    Here is my problem. str_replace is Case SENSITIVE. I don't want it to be. Is there any way around this without writing my own function? Is there perhaps another function I can use?

    My code works wonderfully, that is, if you trust that users who curse will always write the word in the same case

    Oh, and don't say "just uppercase everything" that won't work as I want the user text to remain intact when I dump it to the DB. Otherwise, it would be a good idea.

    Anyone know if they will be writing a stri_replace() function in the next version of PHP?

    Suggestions?
    --There's my 1.5 cents, now where is my change!?!?

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you this:

    1) convert the user text to upper or lowercase before filtering, or

    (Note: You don't have to touch the original variable to do this, for example
    PHP Code:
     run_filter(strtoupper($user_text));
     
    $sql "INSERT .... $user_text
    this way $user_text is unaltered.
    )

    2) Use preg_replace("/your_regex_here/i","replace_text",$user_text), this function also accepts arrays as parameters.
    Last edited by Paul S; Oct 11, 2001 at 12:15.

  3. #3
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I am missunderstanding something here.

    If I use your code, then I run ereg_replace() on the touppered'd version of $user_text. But the variable you are dumping into the DB is the original version, and not the edited one.

    I don't think I follow....
    --There's my 1.5 cents, now where is my change!?!?

  4. #4
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea you're rigth. Ignore first suggestion.
    Picture this, an user has submitted the following text:
    $utext = "This is a BaD WorD text";

    if you want to remove that "bad word", then

    $filtered_text = preg_replace("/bad word/i","",$utext);

    the $filtered_text would contain "This is a text";

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that'd probably make whatever the user said sound pretty messed up.
    you'd be better off using something to count how many characters were removed and replace them with *'s, or replace the swear with another word.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, thats a given. I certainly wouldn't replace the word with a space I'm gonna have a configurable replace string.

    Thanks for the suggestion, I will try it out
    --There's my 1.5 cents, now where is my change!?!?


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
  •