Question: I'm writing some RSS feeds in PHP and stuggling with character-encoding issues. Should I utf8\_encode() before or after htmlentities() encoding? For example, I've got both ampersands and Chinese characters in a description element, and I'm not sure which of these is proper:

```
$output = utf8_encode(htmlentities($source)); or
$output = htmlentities(utf8_encode($source));

```

And why?

Answer: 
It's important to pass the character set to the htmlentities function, as the default is ISO-8859-1:

```
utf8_encode(htmlentities($source,ENT_COMPAT,'utf-8'));

```

You should apply htmlentities first as to allow utf8\_encode to encode the entities properly.

(EDIT: I changed from my opinion before that the order didn't matter based on the comments. This code is tested and works well).
It might be easier to forget htmlentities and use a CDATA section. It works for the title section, which doesn't seem support encoded HTML characters in Firefox's RSS viewer:

```
<title><![CDATA[News & Updates  " > » ☂ ☺ ☹ ☃  Test!]]></title>

```