I have changed the charset to utf8 to no effect, and I am not modifying the content before parsing it, the whole block of code for that section is.
<?php
$query="SELECT xf_post.thread_id, xf_thread.title, xf_thread.reply_count, xf_thread.view_count, xf_post.username, xf_post.user_id...