htmlentities变量不起作用
问题描述:
我正在寻找一种方法,我可以使用代码片段,但安全地将它们插入数据库并将它们拉出。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不符合$符号什么是最好的方法呢?
感谢
答
什么情况是,它试图“解析”在字符串中的“$文”:您正在使用"
,这意味着任何字符串被替换,如果它是一个变量,它不是。
逃避所有$
的一个\
,或使用'
(但你需要躲避'
ofcourse)。
例如:
<?php $snippet = htmlentities("<?php
define ('EMOTICONS_DIR', '/images/emoticons/');
function BBCode2Html(\$text) {
\$text = trim(\$text);
// BBCode [code]
?>"); ?>