Question

I have read their documentation, but I still don't get when to use each of them and their difference.

Let's consider the situation of having a general string in a variable and needing to echo it inside HTML code. If it has any HTML markup in it, I want it converted to HTML code (< replaced by &lt;, & replaced by &amp;. If it has UTF special chars that aren't available in HTML code, it's replaced by HTML number ( replaced by &#8226;).

What's the best function for that?

A harder need: unprintable chars, like \n, char(10), char(13), etc, be replaced by their number code, in the case the string is printed inside <pre> or any special textarea so that the string be dumped.

Was it helpful?

Solution

htmlentities is a workaround for not having set the character type of the document properly. htmlspecialchars is the correct function to use for merely writing text into an HTML document.

As to your second question, I think you're looking for addcslashes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top