0

データベースに保存されているよりも、いくつかの禁止された単語があります。

私がしなければならないことは、それらすべてを承認された新しい単語に置き換えることです。

私はそのようなことをしました

//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;

関数は別のファイルにあります

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

私が抱えている問題は、禁止された単語が 1 つだけ置き換えられることです。すべての単語を置き換えることができればいいのにと思います。

どんな種類の助けも大歓迎です。

4

4 に答える 4

2

各 str_replace の後にバッファーに $new 値を指定する必要があります。そうしないと、最後に最後の検閲のみが取得されます

while ($censure = mysql_fetch_assoc($mots_interdits)):
    $new = str_replace($censure['mot'], $censure['mot2'], $buffer);
    $buffer = $new
endwhile;
于 2014-04-07T13:46:44.543 に答える
1

str_replace 関数は $buffer を入力として使用していますが、それは変更されません。str_replace 関数への入力として、現在の既に変更された文字列を使用していることをループが反復するときに確認する必要があります。次のようなことを試してください:

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

//次の記事について echo $buffer;

于 2014-04-07T13:49:10.780 に答える
1

単語の配列も使用できます。

$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);
于 2014-04-07T13:42:38.963 に答える
-1

str_replace() の代わりに preg_replace() を使用してください。

 preg_replace($censure['mot'], $censure['mot2'], $buffer, 1); 
于 2014-04-07T13:42:29.710 に答える