htmlentities变量不起作用

时间:2011-06-04 12:48:42

标签: php code-snippets html-entities

我正在寻找一种方法,我可以使用代码片段,但安全地将它们插入数据库并将它们拉出来。

我有以下一段代码。

    <?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

哪位对我说这些问题并不适用于$ sign什么是最好的解决方法?

由于

1 个答案:

答案 0 :(得分:2)

它会尝试“解析”字符串中的“$ text”:您正在使用",这意味着任何字符串都会被替换,就像它是一个变量一样,它不是'吨。

使用$逃脱所有\,或使用'(但之后您需要逃离'当然。)

例如:

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

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

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

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