0
$Header = 'test';
$Text = '<p class="test">Text></p><p class="test">Text></p>';

$message ='
        <html>
        <head>
        <title>'.$Header.'</title>

        </head>
        <body>';

        $message .='
        <p>'.$Header.'</p>
        <p></p>';
        $message .="$Text";

        $message .='
        </body>
        </html>
        ';

echo $message; outputs:

test
<p class="test">Text></p><p class="test">Text></p>

As we can see, the line <p class="test">Text></p><p class="test">Text></p> is displayed as string, not as an html entity.

How could I add $Text to $message variable so as I get it as an html entity ?

P.S.: the people I understand that for some, it is an easy question, but if You vote against it, please write the answer. many people do not know the answer to this question.

  • i thing you are trying to embed html inside php, if so here is an easy way.. http://stackoverflow.com/questions/722379/can-html-be-embedded-inside-php-if-statement ... hope it helps. thank you.. – Sathya Baman May 21 '14 at 09:55
  • @user3234352 this answer not use `if else` –  May 21 '14 at 11:49
  • I think you are not showing origin of your message. Because `echo "Red text"` will print out `Red text` in red color as html – Justinas May 26 '14 at 14:14

2 Answers2

-1

You've almost given the answer yourself: use the function htmlentities.

$message .= htmlentities($Text);

It will translate <, > and a bunch of other characters to their respective HTML entities.

An alternative is htmlspecialchars. It also encodes the special characters of HTML (brackets and quotes), but not diacritics.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
-3

try this

$message .= $Text ;

instead of

$message .="$Text";
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51