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 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using array to replace text

    Now, this is a tricky one for me so I hope someone know if there is a solution to this:


    I'm currently using this code to search an array for a word and if found replace that word with ****. Now, can I use an array to specify both the bad word AND a word to replace it with?

    My current code:

    PHP Code:
    function find_replace_happy ($word){

    $badwords = array(

    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword' 

    ); 
        

    $word str_replace($badwords,'****'$word);
        return 
    $word;


  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe like this:
    PHP Code:
    function find_replace_happy ($word){

    $badwords = array(

    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword',
    => 'badword' 

    ); 
        
    $goodwords = array(

    => 'goodword',
    => 'goodword',
    => 'goodword',
    => 'goodword',
    => 'goodword',
    => 'goodword',
    => 'goodword',
    => 'goodword' 

    ); 

    $word str_replace($badwords,$goodwords$word);
        return 
    $word;

    (At least, I think that'll work).

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mean like this:

    PHP Code:
    function find_replace_happy ($word){

    $badwords = array(

    => 'test',
    => '****',
    => 'badword',

    );

    $goodwords = array("happy""sad""yummy");

    for (
    $i=0$i<=count($badwords); $i++)
        for (
    $j=$i$j<=$i$j++)
            
    $word str_replace($badwords[$i],$goodwords[$j], $word);
            return 
    $word;


  • #4
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah I didn't know that str_replace can take the array index automatically without looping.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Yes, it can. However the methods described are not what you want.

    Your original code that you posted should work.

    When str_replace is fed an array of searches, and a string to replace with it will use that for every entry it finds in the search list.

    From the manual: "If search is an array and replace is a string, then this replacement string is used for every value of search."

    PHP Code:
    function find_replace_happy($word) {
        
    $badwords = array(
            
    => 'badword',
            
    => 'badword',
            
    => 'badword',
            
    => 'badword',
            
    => 'badword',
            
    => 'badword',
            
    => 'badword',
            
    => 'badword' 
        
    ); 

        return 
    str_replace($badwords'****'$word);


  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, got it to work

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    BonRouge's code is a thing-a-beauty if you want to customize the replacement words so I wouldn't say it's not what you want.

    Example replacement words:

    farg
    shizniz
    durn
    gull durn
    fuzzy
    heck
    wench
    kitty
    son-of-unwed-mother
    rooster


  •  

    Posting Permissions

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