Php str_ireplace function help

Discussion in 'PHP' started by Om ji Kesharwani, Dec 17, 2010.

  1. #1
    
    <?
    $text="this is class";
    $text  = str_ireplace("ass", "a**", $text);
    echo $text;
    ?>
    
    PHP:
    Output: this is cla**
    The above code replace class as cla**
    But i want to replace only exact word ass.

    I need the output should be: this is class.
     
    Om ji Kesharwani, Dec 17, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    
    $text="this is an ass way class, but as ass classes go it should work";
    $text = preg_replace('#\bass\b#', 'a**', $text);
    echo $text;
    
    PHP:
     
    Last edited: Dec 17, 2010
    MyVodaFone, Dec 17, 2010 IP
  3. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but the scenario is:


    $text="this is an ass way class, but as dog hotdog classes go it should work";
    $badword=array('ass','dog');
    $replace_with=array('a**','d**');
    $text = preg_replace('#\bass\b#', 'a**', $text);
    echo $text;

    how to pass array in this line $text = preg_replace('#\bass\b#', 'a**', $text);
     
    Om ji Kesharwani, Dec 17, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    
    $text="this is an ass way class, but as dog hotdog classes go it should work";
    $badword=array('ass' => 'a**','dog' => 'd**');
    foreach($badword as $key=>$value)
    {
    $text = preg_replace('#\b'.$key.'\b#',$value, $text);
    }
    echo $text;
    
    PHP:
     
    tvoodoo, Dec 18, 2010 IP
  5. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #5
    
    <?
    $text="this is class";
    $text  = str_ireplace(" ass ", " a** ", $text);
    echo $text;
    ?>
    
    Code (markup):
     
    shofstetter, Dec 18, 2010 IP
  6. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Nice, I done that but the code was lengthy you given the simplest one
    but there should one change

    <?
    $text="this is class";
    $text = str_ireplace("ass ", "a** ", $text);
    echo $text;
    ?>

    Replace left whitespace other wise first word will not match
     
    Om ji Kesharwani, Dec 20, 2010 IP
  7. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #7
    
    <? 
    $badwords = array("ass","a$$","a55","Ass","ASS","A$$","A$$");
    $text="this is an ass way class, but as dog hotdog classes go it should work";
    $clean = ReplaceBadWords($text,$badwords,"a**);
    echo $clean;
    
    function ReplaceBadWords($str, $bad_words, $replace_str){
        if (!is_array($bad_words)){ $bad_words = explode(',', $bad_words); }
        for ($x=0; $x < count($bad_words); $x++){
            $fix = isset($bad_words[$x]) ? $bad_words[$x] : '';
            $_replace_str = $replace_str;
            if (strlen($replace_str)==1){ 
                $_replace_str = str_pad($_replace_str, strlen($fix), $replace_str);
            }
            $str = preg_replace('/'.$fix.'/i', $_replace_str, $str);
        }
        return $str;
    }
    ?>
    
    Code (markup):
    You could also modify this function to use key->value badword->replacement as tvoodoo shows in his code. To be able to handle a wider variety of bad words:)
     
    shofstetter, Dec 20, 2010 IP