1

I have HTML tags present inside php variable. I want to print the values as it is. Here is what i tried.

$str = "A 'quote' is <b>bold</b>";

echo htmlentities($str);
// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;

echo $str; //out puts as follows

A 'quote' is bold

But i want to print it as

 A 'quote' is <b>bold</b>

Also, Is there any setting can be done at the TOP of the php page, so that i dont need to use it in every php variables ?

7
  • 2
    Dont use html ebtities. Just echo $str Commented Feb 23, 2014 at 12:40
  • why not just echo $str Commented Feb 23, 2014 at 12:40
  • 1
    If you want to send HTML tags verbatim, don't use htmlentities(). If you want to show the tags in the browser as literal text, you need to escape it as you are doing currently. Please clarify what you actually want to do Commented Feb 23, 2014 at 12:40
  • @Bojangles : I have updated my question to say what i want Commented Feb 23, 2014 at 12:43
  • No you haven't. What are you doing with this text? Echoing it to a browser or something else? Commented Feb 23, 2014 at 12:47

5 Answers 5

7

Just use

 $str = "A 'quote' is <b>bold</b> ";

echo htmlspecialchars($str);

because htmlentities convert some characters to HTML entities.

You should instead use htmlspecialchars

It replaces characters as below:

'&' (ampersand) becomes &amp;
'"' (double quote) becomes &quot; when ENT_NOQUOTES is not set.
"'" (single quote) becomes &#039; only when ENT_QUOTES is set.
'<' (less than) becomes &lt;
'>' (greater than) becomes &gt;

You can check php fiddle here

Sign up to request clarification or add additional context in comments.

6 Comments

It is printing as A 'quote' is bold (bold in bold format). Please read my question
hey don't downvote Ok. Its you who didn't post the question correctly and then edited it to make sense
I just edited echo $str output. but my original question says A 'quote' is <b>bold</b> is expected output. I did not change that. Still my requirement is A 'quote' is <b>bold</b> ; Please edit your answer to accept answer
echo htmlspecialchars($str); is not outputing anything to browser
|
3

This should work -

$str = "A 'quote' is <b>bold</b>";
echo "<xmp>".$str."</xmp>";
//Outputs - A 'quote' is <b>bold</b>

EDIT:

<XMP> is deprecated as was pointed out in the comments, and this seems to be the workaround for it(using the <PRE> tag and htmlentities)-

$str = "A 'quote' is <b>bold</b>";
echo "<pre>".htmlentities($str)."</pre>";
//Outputs - A 'quote' is <b>bold</b>

5 Comments

Great ! it seems, i need to set <xmp> everytime for all variables, Is there any setup done at the begining of the page ?
It's an html tag. So a good idea would be use a function, taking the parameter as the input string and returning the above echo.
@Shiva : so what is the replacement for that ?
@Shiva It is old, but it seemed to work for me all the time. Even your thread says so(and it's replacement is debated too). But you are right, it is deprecated.
0

If you want to print HTML, don't use htmlentities.

If this is user input, you should still filter it.

Edit:

If you want the browser to show the text as A 'quote' is <b>bold</b>, htmlspecialchars or htmlentities is the correct function to use as they escape the HTML code and the browser will show the tags as you want.

3 Comments

It is printing as A 'quote' is bold (bold in bold format). Please read my question
The samething I am explaining. I just edited echo $str output. but my original question says A 'quote' is <b>bold</b> is expected output. I did not change that. Still my requirement is A 'quote' is <b>bold</b> ; Please edit your answer to accept answer
A 'quote' is <b>bold</b> is printed and is what you've specified. And then I don't see your problem with htmlentities. This is the output of your code jsfiddle.net/t7Kra
0

What worked for me was to use the PHP HEREDOC syntax to stuff the html into a PHP string --

<pre>
<?php
$str=<<<HEREDOC
html code including most anything (but not more php)
HEREDOC;
echo htmlentities($str);
?>
</pre>

Comments

-1

to make it as setting..

<?php

function my_custom_echo($str)
{
echo htmlspecialchars($str);
}

my_custom_echo("<p>what ever I want </P>")


?>

Comments

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.