HTML:如何阻止用PHP编写的字符串中的html代码在Web页面上显示为Html元素

I am having problem in making html code in a string written PHP from appearing as an html element on the web page. What i really want is to make the html code to show as text on the page. Is there a way to preserve the html code in a string as string on the page?

Use htmlentities().

It changes things like < and > to &lt; and &gt;.
Visually, they're exactly the same to the user but it won't be parsed as HTML tags.

If you are displaying Html submitted by an end user, take it a step further and use http://htmlpurifier.org/ or http://tidy.sourceforge.net/ to remove potential submitted malicious code. For example, prevent some one from entering javascript, or php code, etc. That could in turn be used to cause trouble.