2011-06-04 132 views
1

我正在尋找一種方法,我可以使用代碼片段,但安全地將它們插入數據庫並將它們拉出。htmlentities變量不起作用

我有以下部分代碼。

<?php $snippet = htmlentities("<?php 

define ('EMOTICONS_DIR', '/images/emoticons/'); 

function BBCode2Html($text) { 
    $text = trim($text); 

    // BBCode [code] 
?>"); ?> 

<pre class="prettyprint"> 

<?php echo $snippet; ?> 

</pre> 

但是當我嘗試在瀏覽器中運行代碼時出現以下錯誤。

Notice: Undefined variable: text in C:\xampp\htdocs\prettycss\index.php on line 18 

Notice: Undefined variable: text in C:\xampp\htdocs\prettycss\index.php on line 18 

Notice: Undefined variable: text in C:\xampp\htdocs\prettycss\index.php on line 21 

這對我說htmlentities不符合$符號什麼是最好的方法呢?

感謝

回答

2

什麼情況是,它試圖「解析」在字符串中的「$文」:您正在使用",這意味着任何字符串被替換,如果它是一個變量,它不是。

逃避所有$的一個\,或使用'(但你需要躲避' ofcourse)。

例如:

<?php $snippet = htmlentities("<?php 

define ('EMOTICONS_DIR', '/images/emoticons/'); 

function BBCode2Html(\$text) { 
    \$text = trim(\$text); 

// BBCode [code] 
?>"); ?>