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: 
You want to do `$output = htmlentities(utf8_encode($source));`. This is because you want to convert your international characters into proper UTF8 first, and then have ampersands (and possibly some of the UTF-8 characters as well) turned in to HTML entities. If you do the entities first, then some of the international characters may not be handled properly.

If none of your international characters are going to be changed by utf8\_encode, then it doesn't matter which order you call them in.
After much trial & error, I finally found a way to properly display a string from a utf8-encoded database value, through an xml file, to an html page:

```
$output = '<![CDATA['.utf8_encode(htmlentities($string)).']]>';

```

I hope this helps someone.