问题描述
|
我正在寻找一种方法来使用代码段,但可以安全地将其插入数据库中,然后将其拉回。
我有以下一段代码。
<?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不能在$符号上工作,什么是解决此问题的最佳方法?
谢谢
解决方法
发生的是,它试图\'resolve \'字符串中的\“ $ text \”:您正在使用
\"
,这意味着将替换任何字符串,就好像它是一个变量一样,不是。
使用ѭ4来转义所有ѭ3的\,或使用ѭ5来转义(但是您必须先逃避ѭ5的当然)。
例如:
<?php $snippet = htmlentities(\"<?php
define (\'EMOTICONS_DIR\',\'/images/emoticons/\');
function BBCode2Html(\\$text) {
\\$text = trim(\\$text);
// BBCode [code]
?>\"); ?>