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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts

    comments posted - wish to replace obscene words with hashed and stared (substitute)

    I have been trying to figure this out but not quiet sure how to do this effectively.

    say some one posts a comment to a forum and they have words that are not suitable for family viewing, i wish to have these words substituted with other words like for example, s*it instead of the obvious word that is was.

    i think i need the words to be in an array, maybe from a database where the list can be added to when new words are used that need changing.

    any ideas on how i actually change the words in a long variable from one word to another.


    thanks in advance for your help.


    and happy new year.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $obscene = array('word1''word2', ....);
    $regobscen = array();
    foreach(
    $obscen as $word){
      
    $regword '/' $word '/';
      
    $regobscen[$tegword] = preg_replace('/./','*',$word);
    }
    $what array_keys($regobscene);
    $with array_values($regobscene);
    $comment preg_replace($what,$with,$comment); 
    I don't test it and I don't check the sintax.

    This is men against the machine, I bet you can't win. If you discover that somebody use 'aba' and you add to
    obscene words, you'll find later spelled as 'a b a', 'a$b a' or 'A b A', or other variants.

    best regards and happy new year
    Last edited by oesxyl; 12-30-2007 at 11:50 AM.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You could shorten that a bit like this:
    PHP Code:
    $replace = array('word1''word2');
    foreach(
    $replace as $k => $word)
    {
        unset(
    $replace[$k]);
        
    $replace[$word] = str_repeat('*'strlen($word));
    }

    $comment str_replace(array_keys($replace), array_values($replace), $comment); 

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Inigoesdr View Post
    You could shorten that a bit like this:
    PHP Code:
    $replace = array('word1''word2');
    foreach(
    $replace as $k => $word)
    {
        unset(
    $replace[$k]);
        
    $replace[$word] = str_repeat('*'strlen($word));
    }

    $comment str_replace(array_keys($replace), array_values($replace), $comment); 
    agree with that, but must be corrected because the items of array wich is first argument of the preg_replace must be beetwen '/'.
    solution is simple:

    PHP Code:
    $replace = array('/word1/''/word2/', ...); 
    and

    PHP Code:
        $replace[$word] = str_repeat('*'strlen($word)-2); 
    PS: I think that preg_replace_callback could be a beter way.

    best regards and happy new yesr

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by oesxyl View Post
    agree with that, but must be corrected because the items of array wich is first argument of the preg_replace must be beetwen '/'.
    solution is simple:

    PHP Code:
    $replace = array('/word1/''/word2/', ...); 
    and

    PHP Code:
        $replace[$word] = str_repeat('*'strlen($word)-2); 
    I didn't use preg_replace().
    Quote Originally Posted by oesxyl View Post
    PS: I think that preg_replace_callback could be a beter way.
    You could use preg_replace_callback(), but it's not necessary here.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Inigoesdr View Post
    I didn't use preg_replace().
    sorry and thank you for replay, I was blind,

    my previos replay is incorrect and useles,

    You could use preg_replace_callback(), but it's not necessary here.
    I'm thinking to a shorter and maybe complex solution, I mean to replace
    misspeled words which avoid ban and other thing.
    Anyway this could become more complex then moderating comments,

    best regards and happy new year


  •  

    Posting Permissions

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