DCHP
DCHP

Reputation: 1131

htmlentities variables not working

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

Upvotes: 1

Views: 650

Answers (1)

Nanne
Nanne

Reputation: 64399

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]
?>"); ?>

Upvotes: 2

Related Questions