views:

83

answers:

3

Hi.

Consider strip_tags() .

strip_tags("<b>TEXT</b>");

Output:

TEXT

But what if i want to nullify the effect of the tags but display them as well?

Output:

<b>TEXT</b>

Would i have to use preg_replace() ? Or is there a more elegant solution available?

Thanks :D

+1  A: 

You could always just replace < with &lt; and > with &gt;, yeah?

Chris
will this also eliminate the need to use strip_slashes in the 1st place?
wretrOvian
@wretrOvian strip_tags you mean? it is up to you.
Col. Shrapnel
+6  A: 

You can HTML encode the string via htmlspecialchars:

htmlspecialchars("<b>TEXT</b>");
RedFilter
+3  A: 

You can easily convert characters to their HTML entity using htmlspecialchars or htmlentities. Make sure you check the PHP manual to determine what is most appropriate to your data, as both functions operate slightly differently.

You can then reverse the encoding with htmlspecialchars_decode and html_entity_decode - again, check which is most appropriate for your data.

akamike
htmlspecialchars is the only one usable function of these
Col. Shrapnel
What do you mean? They are all valid PHP functions, and each have their purpose. Perhaps the OP does only need `htmlspecialchars`, but that does not invalidate the usefulness of the other functions in similar situations.
akamike