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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    434
    Thanks
    7
    Thanked 3 Times in 3 Posts

    how to replace one phrase with another using arrays

    I wish to filter inappropriate words using an array method.
    I currently have...

    $badwords = array("'word1'i", "'word2'i", "'word3'i"
    );

    That I have in a function that basically returns, good or bad, and if bad the page says so and stops the phrase being added until they change it.


    So lets say someone enters in the field..
    'I am a badword'
    I wish to use an array method that will replace the badword with something else.

    like so...


    badword1 to goodword1
    badword2 to goodword2

    It would also need to be case insensitive.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    There's no array function in PHP that does that - simply replacing a value in an array if a pre-defined string is found.

    You'll just have to use a lo0p to do that for you. E.g a for loop

    This is kinda crude but it's along the kines for what you should use

    PHP Code:
    for($i=0$i<=count($your_array))
    {

    if(
    $your_array[$i] == 'string1' || $your_array[$i] == 'string1' || $your_array[$i] == 'string1'){

    die(
    "Bad word used');

    }



  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You can use str_replace for this:
    PHP Code:
    $aBadWords = array('badword1''badword2');
    $aGoodWords = array('goodword1''goodword2');

    print 
    str_replace($aBadWords$aGoodWords$input); 
    str_ireplace can be done case insensitively. If you need more complex checks such as containing a phrase, then a pcre with preg_replace would be easier.

  • Users who have thanked Fou-Lu for this post:

    needsomehelp (11-05-2012)

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Posts
    434
    Thanks
    7
    Thanked 3 Times in 3 Posts
    This one looks like it will do the job I need it for, thanks.

    I had been racking my brain for a way to use an array that contains the badword and good work so the script would have checked for the first and replaced with the associated good word. but this method will work for me i hope.

    thanks again.

    Quote Originally Posted by Fou-Lu View Post
    You can use str_replace for this:
    PHP Code:
    $aBadWords = array('badword1''badword2');
    $aGoodWords = array('goodword1''goodword2');

    print 
    str_replace($aBadWords$aGoodWords$input); 
    str_ireplace can be done case insensitively. If you need more complex checks such as containing a phrase, then a pcre with preg_replace would be easier.


  •  

    Posting Permissions

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