【问题标题】:replace content in a page php替换页面php中的内容
【发布时间】:2014-05-19 19:11:25
【问题描述】:

我有一些违禁词,比存储在数据库中。

我需要做的就是用授权的新词替换所有这些。

我做过类似的事情

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

我的麻烦是它只能替换一个禁用词,我希望它可以替换所有单词。

我们将不胜感激。

【问题讨论】:

    标签: php str-replace ob-start ob-get-contents


    【解决方案1】:

    使用 preg_replace() 代替 str_replace(),

     preg_replace($censure['mot'], $censure['mot2'], $buffer, 1); 
    

    【讨论】:

      【解决方案2】:

      你也可以使用单词数组:

      $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);
      

      【讨论】:

        【解决方案3】:

        您必须在每次 str_replace 之后将 $new 值赋给缓冲区,否则您只会在最后得到最后一次审查

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

        【讨论】:

          【解决方案4】:

          您的 str_replace 函数使用 $buffer 作为输入,它不会修改它。您需要确保在循环迭代时使用当前已修改的字符串作为 str_replace 函数的输入。试试这样的:

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

          //关于 affiche le nouveau contenu 回声 $buffer;

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-15
            • 2011-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-08-10
            相关资源
            最近更新 更多