…with certain input that contains invalid UTF-8 characters, when added to text nodes or attribute values.
It’s probably a problem with the version of libxml included in OS X 10.5 (Leopard). It outputs successfully (including the invalid characters) with the same script on CentOS Linux.
$text = iconv('UTF-8', 'UTF-8//IGNORE', $text);
This will strip any invalid UTF-8 characters from
$text. (I’m assuming here that
$text is already supposed to be UTF-8 in your application.)