0

I am trying to print php variable which contains html tag in it. I want to display it as html but it is print it as plain text. Below is my code:

$final= $first . $second . $third;
echo $final;
echo htmlentities($final);
echo htmlspecialchars($final);

$first value is <iframe

$second value is width="960" height="540"

$third value is src="" allowfullscreen="true"></iframe>

so $final combine string of all 3 variable which I intend to display it as an iframe. But it just display text as below:

<iframe width="960" height="540" src="" allowfullscreen="true"></iframe>
1
  • 3
    Why do you use htmlentities and htmlspecialchars then? Commented Feb 7, 2019 at 14:25

2 Answers 2

2

I think that it's happening because you are using the incorrect method htmlentities OR htmlspecialchars. Remember that those methods converts HTML into characters. If you want to print the HTML from String, you need to use html_entity_decode.

echo html_entity_decode($final);
Sign up to request clarification or add additional context in comments.

Comments

0

Like this :

$first = '<iframe';
$second = ' width="960" height="540" ';
$third = 'src="" allowfullscreen="true"></iframe>';

$final= $first . $second . $third;
echo $final;

OR

Convert HTML entities to their corresponding characters write down below code http://php.net/manual/en/function.html-entity-decode.php

echo html_entity_decode($final);

10 Comments

Can you explain why you think that adding spaces here and there solve the problem of escaped HTML tags?
@NicoHaase no need for downvote for that. He also removed the htmlentities and htmlspecialchars. And the formatting of the backicks in question doesn't show a space after <iframe which he's right about, He thinked that it'll show up as <iframewidth.
@NicoHaase because without adding space check in browser html tag might be look like this <iframewidth="960" height="540" src="" allowfullscreen="true"></iframewidth="960">
@TahaPaksu that's why I've asked for an explanation of that code. The code in the question shows that the case of strings connected without using spaces between them (where the result would contain iframewidth) does not occur
I still wouldn't downvote a new contributor's answer even if he writes junk without getting an explanation of himself. Instead I would try to teach him how to correct his mistakes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.