phpstr-replaceob-startob-get-contents

replace content in a page php


I have some forbidden words, than are stored in database.

the things I need to do is to replace all of them byt a new words authorized.

I did something like that

//Inclusion du fichier à parser
require_once GETCASH_BASE_PATH . '/GESTION/pages_html/index.phtml'; // Layout principal
//Récupération du contenu
$buffer = ob_get_clean();


//Modification du contenu
$mots_interdits = censure();
while ($censure = mysql_fetch_assoc($mots_interdits)):
    $new = str_replace($censure['mot'], $censure['mot2'], $buffer);
endwhile;
//On affiche le nouveau contenu
echo $new;

the function is located in an other file

/**
 * fonction qui requete la censure
 * @return type
 */
function censure() {
    $query = "SELECT `mot`, `mot2` FROM `censure`";
    $result = mysql_query($query);
    return $result;
}

The trouble I have is that it replace only one forbidden words, I wish it could replace all words.

anykind of help will be much appreciated.


Solution

  • You have to give the $new value to the buffer after each str_replace, or you will only get the last censuration at the end

    while ($censure = mysql_fetch_assoc($mots_interdits)):
        $new = str_replace($censure['mot'], $censure['mot2'], $buffer);
        $buffer = $new
    endwhile;