htmlentities 变量不起作用

发布于 2024-11-11 23:37:00 字数 729 浏览 7 评论 0原文

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

我有以下代码。

    <?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2024-11-18 23:37:00

发生的情况是,它尝试“解析”字符串中的“$text”:您正在使用 ",这意味着任何字符串都将被替换为变量,但事实并非如此。 t.

使用 \ 转义所有 $ ,或使用 ' (但随后您需要转义 ' > 当然)

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

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

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

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

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:

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

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

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

// BBCode [code]
?>"); ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文