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 不适用于 $ 符号,解决这个问题的最佳方法是什么???
谢谢
I am looking for a way i can use code snippets but safely insert them into a database and pull them back out.
I have the following piece off code.
<?php $snippet = htmlentities("<?php
define ('EMOTICONS_DIR', '/images/emoticons/');
function BBCode2Html($text) {
$text = trim($text);
// BBCode [code]
?>"); ?>
<pre class="prettyprint">
<?php echo $snippet; ?>
</pre>
But when i try to run the code in the browser i get the following errors.
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
Which says to me the the htmlentities is not working on $ signs what is the best way around this ???
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生的情况是,它尝试“解析”字符串中的“$text”:您正在使用
"
,这意味着任何字符串都将被替换为变量,但事实并非如此。 t.使用
\
转义所有$
,或使用'
(但随后您需要转义'
> 当然)。
What happens is that it tries to 'resolve' the "$text" in your string: you're using
"
, which means that any string is replaced as if it was a variable, which it isn't.Escape all
$
's with a\
, or use'
(but then you need to escape the'
ofcourse).For instance: